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.
5.4.8-NTS-VC9 (Windows FastCGI)
on my local machine and5.3.10-1ubuntu3.4
on my testing server. The problem occurs on both. – CorenaAND
and&&
is different. That's why there are so many parenthesis in there. Our code style guide says that we should useAND
andOR
with extra parens as needed because the person who wrote it thought that would be easier to read. The only difference between them is the precedence, and the parens fix that. – CorenaModel_ActionPlan
we can only guess what's going on. – Checkedvar_dump
the object, you can see that_object['status']
(the place that the magic method is looking for its data) is set. So the__isset
method is the correct one and the__get
is the problem. Note that I mentioned that callingparent::__get('status')
does work. – Corena$this->status
works elsewhere in your subclass...have you tried generating stack traces of uses of$this->status
to the place where it doesn't work? Perhaps its something to do with the context of your use (ie, some other internal state in Mongo_Document). – DesiccateModel_Game->close
. That loads up all of the questions in this game and callsModel_Question->close
on each one. Each question loads all of the connected Action Plans and callsModel_ActionPlan->close
. Note thatModel_Question
also hasstatus
checked - and that one works properly! – CorenaMongo_Document
class the$name
in the method__get
is indeed 'status' (or to be flake, it enters the parents__get
method at all?) and if so, wether it enters the firstif
statement, or in other words it reaches the end of__get
. Also does$this->load()
in theMongo_Document::__get
get called when using$this->status
? – Fibrilliform__get
it just throws the error. – Corena