Re: The inconsistencies/flaws of PHP5's object model
| From: | Julien Pauli | Date: | Thu, 19 Nov 2009 18:32:56 +0000 |
| Subject: | Re: The inconsistencies/flaws of PHP5's object model | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-46111@lists.php.net to get a copy of this message | ||
On Wed, Nov 18, 2009 at 8:59 PM, Stanislav Malyshev <stas@zend.com> wrote:
> Hi!
>
>> I've just occured a syntax problem in the following script:
>>
>> <?php
>> class C {
>> public $n = 1;
>> }
>> $o = new C();
>> $o->f = function () use ($o) {
>> echo $o->n;
>> };
>> $o->f();
>> ?>
>>
>> The result of this script is "Fatal Error: Call to undefined method
>> C::f()".
>> I don't know this is the expected result. After trying more tests of
>
> Yes, this is the expected result. PHP is not Javascript, sorry :) Methods
> and properties are different things in PHP, so the syntax assumes you refer
> to method when you call $o->f(). You could use other syntaxes if you need to
> use a property as callable (see call_user_function, etc.).
>
call_user_func(array($o,'f')); leads to fatal error, I think the same
internal mechanisms than $o->f() happens in call_user_func : the
closure gets ignored but like Stas said : this could lead to problems
with __get() and __call().
Anyway, for your use case to work, you should call $o->f->__invoke();
or call_user_func($o->f);
Julien.Pauli