On Wed, Nov 18, 2009 at 8:59 PM, Stanislav Malyshev <s...@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

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to