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.
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.
Or you could utilize the
__get()
method to access inaccessible properties... I believe this could be added to theBaseObject
class and fetch protected properties from the child class. Since the same class visibility policy should apply toBaseObject
you could define the__get()
method private or protected.how about a getProperty() function in the child object that returns $this->unretrievableProperty
The best possible answer to fix this is to use reflections.
Example:
try it like this: