Re: function call chaining
| From: | Rasmus Lerdorf | Date: | Tue, 19 Jan 2010 17:39:34 +0000 |
| Subject: | Re: function call chaining | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-46816@lists.php.net to get a copy of this message | ||
Stanislav Malyshev wrote:
> Hi!
>
> I wrote a small patch that enables this kind of syntax in PHP:
>
> foo()();
>
> What it means is that if foo() returns callable value (which probably
> should be function name or closure) then it would be called. Parameters
> and more than two sets of () work too.
> Of course, this is mostly useful for doing closures, and that was
> primary drive for implementing it - to make working with closures and
> especially function returning closures easier.
> What does not work currently is $foo->bar()() - since it is surprisingly
> hard to tell parser it's not {$foo->bar}()() - which of course is not
> what I want to do.
>
> The patch is here: http://random-bits-of.info/funcfunc.diff
>
> What do you think? If somebody has better idea btw - maybe make
> something like {foo()}() - and make that work for any expression inside
> {} - that might work too. So, what do you think?
I don't mind the foo()() syntax, especially now that we have closures.
But people are right, we have a longstanding feature request for
$foo()[0] as well, so if we start down this path of adding chaining, we
should do that one as well and see if any others make sense.
I do think the syntax is a bit ugly, but I also think it is clear what
it does and doesn't obscure/mislead the semantics of the call the way
the (new foo)->bar() suggestion does.
Not sure the {} expression syntax is needed. What sort of expressions
do you see being useful here that would need the braces? Stuff like
$a[$idx]() works today without needing any extra syntax. Are you
thinking along the lines of:
{strtolower($a[$idx])}()
or something like that? That would make the "I hate linefeeds and
semi-colons" crowd happy, I guess.
-Rasmus