I have an abstract parent class Mongo_Document
(from mongodb-php-odm) and an inherited class Model_ActionPlan
. Mongo_Document
has magic __isset and __get methods that interact with an array inside the Mongo_Document
class.
I am trying to use the following code (snippet from inside a method of Model_ActionPlan
):
if (isset($this->status))
{
if (($this->status === "closed") AND ($this->close_type != "failure"))
{
return;
}
}
(Note that close_type
is guaranteed to be set if status == 'closed'
.)
The isset
call returns true
and then execution proceeds to the next statement. There, I receive the following error:
Undefined property: Model_ActionPlan::$status
However, if I replace $this->status
with parent::__get('status')
, this code works as expected. Note that everywhere else in the program, I am able to use:
$ap = new Model_ActionPlan($plan_id);
echo $ap->status;
// Prints 'closed' (or 'active') as expected
It is only here, inside the class itself, that this doesn't work.
I looked around and I can't seem to find anywhere that says that magic methods can't be called in the child class. I could use the parent::__get
call instead but I think that is probably the wrong way to do it. Does anyone know if there is a right/better way to do this?
Updated #1 2012-12-16: The full code of the parent class is here on Github.
Updated #2 2012-12-18:
For the people who asked about where or whether it is set properly, the answer is that since calling parent::__get('status')
does work, the problem is obviously not that the variable isn't getting set. The __get()
is getting its data from a private instace variable called _object
. If I var_dump($this)
, I see that $this->_object['status']
does equal the expected "closed" value.
Update #3: The code of the child class is available at https://gist.github.com/4332062. The important part starts on line 69.
I have seen this similar question but that one is about using a parent's magic method to get the child's properties and my issue is using the parent's getter to get the parent's properties.
The __get function in the parent is somewhat complex, so I haven't fully figured out whether the following might be happening or not. If your __get function, once called, is in some way triggering another call back into itself (perhaps with some intervening calls to other functions on the stack), this is exactly what would happen.
See http://php.net/manual/en/language.oop5.overloading.php#55486, which shows the exact same type of error being logged when the getter triggers a call to itself. In that instance, it is pretty easy to spot, but with a more complicated call graph of something like func() to __get() to funcB() to funcC() to __get(), it would not be easy to spot.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With