php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #68182 array("foo", "bar")() is invalid syntax
Submitted: 2014-10-08 07:47 UTC Modified: 2016-11-11 21:55 UTC
From: chx@php.net Assigned:
Status: Open Package: PHP Language Specification
PHP Version: PHP-5.6 OS:
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2014-10-08 07:47 UTC] chx@php.net
Description:
------------
According to the specs, https://github.com/php/php-langspec/blob/master/spec/10-expressions.md#function-call-operator any postfix expression + argument list is a valid function call. 

Looking at postfix expressions https://github.com/php/php-langspec/blob/master/spec/10-expressions.md#general-3 it says array-creation-expression is a postfix expression.


Test script:
---------------
array("foo", "bar")();

Expected result:
----------------
PHP Fatal error:  Class 'foo' not found in Command line code on line 1


Actual result:
--------------
PHP Parse error:  syntax error, unexpected '(' in Command line code on line 1


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-10-08 08:06 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2014-10-08 08:06 UTC] requinix@php.net
Actually it is valid syntax but you need the uniform variable syntax changes in PHP 7 to get it.
http://3v4l.org/TbcgJ
 [2014-10-08 14:54 UTC] cmbecker69 at gmx dot de
> According to the specs, any postfix expression + argument list
> is a valid function call.

No.  While it is syntatically valid, there are further constraints:

| postfix-expression must designate a function, either by being
| its name, by being a value of type string (but not a string
| literal) that contains the function's name, or by being a variable
| whose type is Closure (§§) or a derived type thereof.

array("foo", "bar") doesn't fulfill these constraints.
 [2014-10-08 17:24 UTC] ajf@php.net
In that case, we should change this is master to reflect it being accepted by PHP7's Uniform Variable Syntax.
 [2014-10-08 19:07 UTC] requinix@php.net
-Status: Not a bug +Status: Open
 [2014-10-08 19:07 UTC] requinix@php.net
Yeah, the constraints doesn't mention that arrays are allowed. And

> postfix-expression must designate a function... by being a value of type
> string (but not a string literal) that contains the function's name
UVS allows literals http://3v4l.org/V6tTG

Side note: the sentence immediately after
> The number of arguments present in a function call must be at least as many
> as the number of parameters defined for that function.
should clarify that it's the number of *required* parameters.

Then at the end of the Semantics section,
> If postfix-expression is a string, this is a variable function call
which links to 13-functions.md#variable-functions that says
> If a variable name is followed by the function-call operator () (§§), and
> the value of that variable is a string containing the name of a function
> currently defined and visible, that function will be executed.
which also doesn't include arrays.
 [2015-06-28 22:42 UTC] stas@php.net
-PHP Version: Irrelevant +PHP Version: PHP-5.6
 [2016-11-11 21:55 UTC] nikic@php.net
function-call-expression now has a callable-expression as the LHS, which should accurate represent what is (syntactically) allowed on the LHS.

However, this does not resolve the inaccuracy wrt array *values*.
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Fri Sep 24 20:03:36 2021 UTC