title: PHP: Can't set object property from within object method?
body: <p>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 <em>and set the object's property</em>.</p>
  
  <p>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 <strong>every</strong> time.</p>
  
  <p>I've tried checking the value of the property using is_null, empty, isset.  I'm feeling like a dope.</p>
  
  <pre><code>require_once('dbaccess.class.php');
  
  class User {
      protected $user_id;
      protected $first_name;  
  
  function __construct($user_id) 
      {
      $this-&gt;user_id = $user_id;  
  
      }
  
  function __get($variablename)
      {
      return $this-&gt;$variablename;
      }
  
  function __set($variablename, $variablevalue)
      {
      $this-&gt;$variablename = $variablevalue;
      }
  
  function __destruct()
      {
  
      }
  
  function retrieve_user_first_name() 
      {       
      if(!isset($this-&gt;first_name)) {
          $dbAccess = new DBAccess();
          $strFirstName = $dbAccess-&gt;retrieve_user_first_name($this-&gt;user_id);
          $this-&gt;first_name = $strFirstName;
      }
      else {
          $strFirstName = $this-&gt;first_name;  
      }
      return $strFirstName;
      }
  
  }
  </code></pre>