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;
}
}