title: PHP Accessing a child's private properties in parent
body: <p>I have a parent object that I use for general CRUD in my applications - it has basic save &amp; retrieve methods so I can don't have to reinclude them them in all my objects. Most of my child objects extend this base object. This has worked fine, but I'm finding a problem with retrieving a serialized child object. I use a "retrieve" method in the parent object that creates an instance of the child, then populates itself from the properties of the unserialized child - this means is can "self unserialize" the object.</p>
  
  <p>Only problem is - if the child object has a protected or private property, the parent object can't read it, so it doesn't get picked up during retrieval.</p>
  
  <p>So I'm looking either for a better way to "self unserialize" or a way to allow a parent object to "see" the protected properties - but only during the retrieval process.</p>
  
  <p>Example of the code:</p>
  
  <pre><code>BaseObject {
  
   protected $someparentProperty;
  
   public function retrieve() {
  
    $serialized = file_get_contents(SOME_FILENAME);
    $temp = unserialize($serialized);
    foreach($temp as $propertyName =&gt; $propertyValue) {
      $this-&gt;$propertyName = $propertyValue;
    }     
  
   }
  
   public function save() {
  
      file_put_contents(SOME_FILENAME, serialize($this));
   }
  }
  
  class ChildObject extends BaseObject {
  
   private $unretrievableProperty;  
  
   public setProp($val) {
      $this-&gt;unretrivableProperty = $val;
   }
  }
  
  $tester = new ChildObject();
  $tester-&gt;setProp("test");
  $tester-&gt;save();
  
  $cleanTester = new ChildObject();
  $cleanTester-&gt;retrieve();
  // $cleanTester-&gt;unretrievableProperty will not be set
  </code></pre>
  
  <p>EDITED: Should have said "Private" not protected child properties.</p>