note 107246 deleted from language.oop5.magic by danbrown
| From: | danbrown@php.net | Date: | Thu, 10 May 2012 21:35:21 +0000 |
| Subject: | note 107246 deleted from language.oop5.magic by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-187911@lists.php.net to get a copy of this message | ||
Note Submitter: mightye at gmail dot com
----
osbertv at yahoo dot com: the problem is not that B contains an instance of A, the problem is that
"$b->a()" looks for a method named "a" on $b, and when one is not found, it
calls B's __invoke(). This is an order of operations thing, the compiler couldn't hope to
know whether you meant to call B::a() or B::$a(). The previous one can be compile-time optimized
while the latter one cannot (since there is no guarantee that B::$a is always callable). When there
is a collision of intent like this, the compiler tends to give priority to the optimizable use case
over the unoptimizable use case.
You can either choose to proxy a call to $this->a; inside of B::__invoke() (probably with
something like: return call_user_func_array($this->a, func_get_args()); ), or you can make an
actual method B::a() for the same purpose (which would be faster).
However, either way, it's probably not a great idea to be trying to use properties as though
they were methods. The code this would produce can be very confusing to read. Creating B::a()
solves this readability problem.