Re: [PHP-DEV] Call closure stored as object property directly without use of temporary variable

2012-12-09 Thread Stas Malyshev
Hi!

> As it stands now, calling a method can mean two different things:
> calling the method directly, or handling the method invocation within
> __call(). Checking for a method first, a closure instance next, and then
> falling through to __call() seems like it would have been a reasonable
> approach.

This is the same thing. However, what you mean by "closure instance" is
actually a property access, which is not the same thing as method call.
Think about what happens if you have both __call and __get (or object
implementing both handlers).

-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

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



Re: [PHP-DEV] Call closure stored as object property directly without use of temporary variable

2012-12-08 Thread Marco Pivetta
I'm currently working around this problem by assuming that a Closure
implements `__invoke`:

`$this->someProp->__invoke();`

Not really nice, but this works in both 5.3 and 5.4.

If the suggestion above could cover also `callable` and not just `Closure`
then it would be quite interesting to have that.



Marco Pivetta

http://twitter.com/Ocramius

http://ocramius.github.com/


Re: [PHP-DEV] Call closure stored as object property directly without use of temporary variable

2012-12-08 Thread Adam Jon Richardson
On Sat, Dec 8, 2012 at 5:36 PM, Steve Clay  wrote:

> On 12/8/12 4:48 PM, Adam Jon Richardson wrote:
>
>> call closures that are stored as object properties directly without having
>> to make use of a temporary variable.
>>
> ...
>
>  $o = new stdClass();
>> $o->func = function(){
>>  return 'Yes!';
>> };
>> $o->func();
>>
>
> The following expression avoids PHP's dilemma of distinguishing
> prop/method, but fails because you can't execute an expression:
>
> ($o->func)();
>

I actually tried that very technique before remembering the limitation :)

Interesting idea.

Adam


Re: [PHP-DEV] Call closure stored as object property directly without use of temporary variable

2012-12-08 Thread Adam Jon Richardson
On Sat, Dec 8, 2012 at 5:05 PM, Stas Malyshev wrote:

> Hi!
>
> > Has anyone else wanted this functionality? Has anyone else thought of
> ideas
> > of addressing this (or come to the conclusion it really isn't safely
> > addressable without causing disproportionate amounts of grief?)
>
> Yes, there were people that wanted this functionality, but since having
> the same thing ($foo->bar()) mean two different things (call method
> named "foo" vs. "fetch property named foo and then call it if it's
> callable") is not a good idea this wasn't done.
> In some languages, the options above are the same - i.e. methods and
> properties are actually the same thing - but in PHP is is not so, and
> can be made so without some BC-breaking changes.
>

As it stands now, calling a method can mean two different things: calling
the method directly, or handling the method invocation within __call().
Checking for a method first, a closure instance next, and then falling
through to __call() seems like it would have been a reasonable approach.

That said, as you mention BC-breaking is now an issue.

Thanks,

Adam


Re: [PHP-DEV] Call closure stored as object property directly without use of temporary variable

2012-12-08 Thread Steve Clay

Adam,

On 12/8/12 4:48 PM, Adam Jon Richardson wrote:

call closures that are stored as object properties directly without having
to make use of a temporary variable.

...

$o = new stdClass();
$o->func = function(){
 return 'Yes!';
};
$o->func();


The following expression avoids PHP's dilemma of distinguishing prop/method, but fails 
because you can't execute an expression:


($o->func)();

Similarly if $a is a Closure, $a() works but ($a)() fails.

If these could be made to work, would it break BC? And *should* they be made to 
work?

Steve Clay
--
http://www.mrclay.org/

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



Re: [PHP-DEV] Call closure stored as object property directly without use of temporary variable

2012-12-08 Thread Stas Malyshev
Hi!

> Has anyone else wanted this functionality? Has anyone else thought of ideas
> of addressing this (or come to the conclusion it really isn't safely
> addressable without causing disproportionate amounts of grief?)

Yes, there were people that wanted this functionality, but since having
the same thing ($foo->bar()) mean two different things (call method
named "foo" vs. "fetch property named foo and then call it if it's
callable") is not a good idea this wasn't done.
In some languages, the options above are the same - i.e. methods and
properties are actually the same thing - but in PHP is is not so, and
can be made so without some BC-breaking changes.
-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

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