views:

48

answers:

2

Hi

I have a parent object that I use for general CRUD in my applications - it has basic save & 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.

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.

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.

Example of the code:

BaseObject {

 protected $someparentProperty;

 public function retrieve() {

  $serialized = file_get_contents(SOME_FILENAME);
  $temp = unserialize($serialized);
  foreach($temp as $propertyName => $propertyValue) {
    $this->$propertyName = $propertyValue;
  }     

 }

 public function save() {

    file_put_contents(SOME_FILENAME, serialize($this));
 }
}

class ChildObject extends BaseObject {

 private $unretrievableProperty;  

 public setProp($val) {
    $this->unretrivableProperty = $val;
 }
}

$tester = new ChildObject();
$tester->setProp("test");
$tester->save();

$cleanTester = new ChildObject();
$cleanTester->retrieve();
// $cleanTester->unretrievableProperty will not be set

EDITED: Should have said "Private" not protected child properties.

A: 

how about a getProperty() function in the child object that returns $this->unretrievableProperty

Crayon Violent
Thanks - nice idea but as baseobject won't know the properties of the child, it won't know what to ask for
Hippyjim
A: 

It doesn't seem that same class visibility policy applies to iherited/parent classes. The php documentation does not address this.

I would suggest that you declared the retrieve method static, and fetched the $cleanTester through a static call rather than your current "self unserialize" approach.

static function retrieve() {
  $serialized = file_get_contents(SOME_FILENAME);
  return unserialize($serialized);
}

[...]

$cleanTester = BaseObject::retrieve();

Or you could utilize the __get() method to access inaccessible properties... I believe this could be added to the BaseObject class and fetch protected properties from the child class. Since the same class visibility policy should apply to BaseObject you could define the __get() method private or protected.

BaseObject {
  private function __get($propertyName) {
    if(property_exists($this,$propertyName))
      return $this->{$propertyName};

    return null;
  }
Ivar Bonsaksen
Thanks. The static method seems viable - but for the fact that some of the child objects need to be instatiated, and to do some processing before they can work out the filename they should be retrieved from.The __get is already used, for pretty much that purpose - but as i'm iterating through the properties of the object, again the parent won't know what properties to ask for. I guess I need to look at the iteration method - thanks for the pointer.
Hippyjim