php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #65612 Calling the result of a function call as a function causes a parse error
Submitted: 2013-09-04 08:08 UTC Modified: 2015-03-21 20:42 UTC
Votes:3
Avg. Score:3.0 ± 1.4
Reproduced:3 of 3 (100.0%)
Same Version:1 (33.3%)
Same OS:1 (33.3%)
From: tdammers at gmail dot com Assigned: nikic (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: Irrelevant OS: Any (debian wheezy e.a.)
Private report: No CVE-ID: None
 [2013-09-04 08:08 UTC] tdammers at gmail dot com
Description:
------------
Calling the return value from a function as a function directly (without storing the return value in an intermediate variable) results in a parse error. Apparently, the PHP parser does not understand `foo()()` or `(foo())()`.

Test script:
---------------
<?php

function foo($a) {
	return function ($b) use ($a) {
		print "$a, $b!\n";
	};
}

foo("Hello")("world");


Expected result:
----------------
Program prints "Hello, world!"

Actual result:
--------------
PHP Parse error:  syntax error, unexpected '(' in /home/tobias/test/test.php on line 9

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-09-05 21:15 UTC] aharvey@php.net
-Package: Compile Failure +Package: Scripting Engine problem
 [2013-09-05 21:15 UTC] aharvey@php.net
Relevant RFC (that never went anywhere, AFAIR): 
https://wiki.php.net/rfc/fcallfcall
 [2014-03-06 08:56 UTC] tri at iki dot fi
Looks like a parser issue to me.  Expression result can't be called directly but storing the same result to variable first and then calling the variable is ok.  Should it parse correctly, there should be no difference, because it's basically a same thing.

However foo()() kind of syntax may be somewhat ambiguous and create mystical precedence issues (too tired to figure out now, whether this is really a problem, e.g. "foo()->xxx()()"), so maybe (foo())() would be a better idea.  Anyways neither of which works.

I think this one should be fixed.
 [2015-03-21 20:42 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2015-03-21 20:42 UTC] nikic@php.net
Support in PHP 7, see https://wiki.php.net/rfc/uniform_variable_syntax.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue May 07 23:01:35 2024 UTC