1

This should be so easy. I have a PHP class that has custom methods to return information about objects. I want to use the custom methods to determine whether or not a specific parameter has been set in the object. If it's set, return the property. If not, the method should contact the database to get the information and set the object's property.

However each time I run the code below, it appears that the object's property does not get set. The retrieve_user_first_name method hits the database access object every time.

I've tried checking the value of the property using is_null, empty, isset. I'm feeling like a dope.

require_once('dbaccess.class.php');

class User {
    protected $user_id;
    protected $first_name;  

function __construct($user_id) 
    {
    $this->user_id = $user_id;  

    }

function __get($variablename)
    {
    return $this->$variablename;
    }

function __set($variablename, $variablevalue)
    {
    $this->$variablename = $variablevalue;
    }

function __destruct()
    {

    }

function retrieve_user_first_name() 
    {       
    if(!isset($this->first_name)) {
        $dbAccess = new DBAccess();
        $strFirstName = $dbAccess->retrieve_user_first_name($this->user_id);
        $this->first_name = $strFirstName;
    }
    else {
        $strFirstName = $this->first_name;  
    }
    return $strFirstName;
    }

}
4
  • 1
    What is the code that calls this? What is the code that you have created that has determined that the variable isn't being set? It looks okay to me, at a couple cursory looks. Maybe you're unintentionally instantiating a new object somewhere instead of reusing the old one and that's breaking your app? Commented Feb 25, 2011 at 16:48
  • try 'print_r/vardump'ing the object after the operation ... it looks okay to my eyes. How are you maintaining/persisting the object(s)? Commented Feb 25, 2011 at 16:49
  • Your code works for me. I instantiated an instance of User, then called retrieve_user_first_name() twice. The first time set $this->first_name, the second use the value set the first time. My guess is you are creating a new instance of the object between calls. Commented Feb 25, 2011 at 16:53
  • 1
    One suggestion for clarity: return $this->first_name instead of $strFirstName and get rid of that else block. Also, make sure to indent consistently, since the indenting scheme you posted seems quite confusing to me... Commented Feb 25, 2011 at 17:07

1 Answer 1

1

The code you posted works fine.

I think the problem is that you are not using the same object for your sequential calls in whatever code is accessing this object.

So something like:

$ob = new User();
$ob->retrieve_user_first_name();

$ob = new User();
print $ob->first_name;

is happening. Obviously, it's probably not so blatant, but that's the simplest explanation for your problem.

The only other option, given the code you posted, is that all of your database calls are returning empty strings or failing, so the variable is never getting updated. This is a very good possibility, so check both of these options in your code.

Sign up to request clarification or add additional context in comments.

1 Comment

OMFG. Duh. Yes, this is it. I just tried handing off the object to another page and it works just fine. Friday morning moron. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.