FAQ

[PHP-BUG] #50003 [NEW]: Allow constructions like func()[0]

Melfar at gmail dot com
Oct 26, 2009 at 6:45 pm
From: melfar at gmail dot com
Operating system: agnostic
PHP version: 5.3.0
PHP Bug Type: Feature/Change Request
Bug description: Allow constructions like func()[0]

Description:
------------
PHP parser doesn't allow for array index operation to be used on a
result of a function call. I propose a patch to allow such a
construction.

Reproduce code:
---------------
<?php
function car_brands() {
return array("chevy", "hummer");
}
print car_brands()[1] . "\n";
?>


Expected result:
----------------
"hummer"



Actual result:
--------------
Parse error: syntax error, unexpected '[' in test.php on line 5

--
Edit bug report at http://bugs.php.net/?id=50003&edit=1
--
Try a snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=50003&r=trysnapshot52
Try a snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=50003&r=trysnapshot53
Try a snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=50003&r=trysnapshot60
Fixed in SVN: http://bugs.php.net/fix.php?id=50003&r=fixed
Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=50003&r=needdocs
Fixed in release: http://bugs.php.net/fix.php?id=50003&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=50003&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=50003&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=50003&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=50003&r=support
Expected behavior: http://bugs.php.net/fix.php?id=50003&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=50003&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=50003&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=50003&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=50003&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=50003&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=50003&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=50003&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=50003&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=50003&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=50003&r=mysqlcfg
reply

Search Discussions

5 responses

  • Melfar at gmail dot com at Oct 26, 2009 at 6:47 pm
    ID: 50003
    User updated by: melfar at gmail dot com
    Reported By: melfar at gmail dot com
    Status: Open
    Bug Type: Feature/Change Request
    Operating System: agnostic
    PHP Version: 5.3.0
    New Comment:

    Not sure how to attach a file. The proposed patch is as follows.



    --- a/Zend/zend_language_parser.y.orig 2009-10-26 18:13:56.000000000
    +0300
    +++ b/Zend/zend_language_parser.y 2009-10-26 21:20:19.000000000
    +0300
    @@ -47,7 +47,7 @@
    %}

    %pure_parser
    -%expect 2
    +%expect 6

    %left T_INCLUDE T_INCLUDE_ONCE T_EVAL T_REQUIRE T_REQUIRE_ONCE
    %left ','
    @@ -874,6 +874,11 @@ variable_class_name:
    ;

    base_variable_with_function_calls:
    + raw_base_variable_with_function_calls { $$ = $1; }
    + | raw_base_variable_with_function_calls '[' dim_offset ']'
    { fetch_array_dim(&$$, &$1, &$3 TSRMLS_CC); }
    +;
    +
    +raw_base_variable_with_function_calls:
    base_variable { $$ = $1; }
    function_call {
    zend_do_begin_variable_parse(TSRMLS_C); $$ = $1; $$.u.EA.type =
    ZEND_PARSED_FUNCTION_CALL; }
    ;


    Previous Comments:
    ------------------------------------------------------------------------

    [2009-10-26 18:45:24] melfar at gmail dot com

    Description:
    ------------
    PHP parser doesn't allow for array index operation to be used on a
    result of a function call. I propose a patch to allow such a
    construction.

    Reproduce code:
    ---------------
    <?php
    function car_brands() {
    return array("chevy", "hummer");
    }
    print car_brands()[1] . "\n";
    ?>


    Expected result:
    ----------------
    "hummer"



    Actual result:
    --------------
    Parse error: syntax error, unexpected '[' in test.php on line 5


    ------------------------------------------------------------------------


    --
    Edit this bug report at http://bugs.php.net/?id=50003&edit=1
  • Johannes at Oct 26, 2009 at 8:58 pm
    ID: 50003
    Updated by: johannes@php.net
    Reported By: melfar at gmail dot com
    -Status: Open
    +Status: Feedback
    Bug Type: Feature/Change Request
    Operating System: agnostic
    PHP Version: 5.3.0
    New Comment:

    The idea was often discussed, but nobody found a fully satisfying
    solution, yet.

    Yours is limited to one array dimension.

    <?php
    function foo() {
    return array(array(42));
    }

    echo foo()[0][0];
    ?>

    Will for instance not work but should be supported.

    I also think it's likely to cause memory problems in edge cases while I
    wasn't able to create one in my quick tests.

    Anyidea aobut the above problem?


    Previous Comments:
    ------------------------------------------------------------------------

    [2009-10-26 18:47:23] melfar at gmail dot com

    Not sure how to attach a file. The proposed patch is as follows.



    --- a/Zend/zend_language_parser.y.orig 2009-10-26 18:13:56.000000000
    +0300
    +++ b/Zend/zend_language_parser.y 2009-10-26 21:20:19.000000000
    +0300
    @@ -47,7 +47,7 @@
    %}

    %pure_parser
    -%expect 2
    +%expect 6

    %left T_INCLUDE T_INCLUDE_ONCE T_EVAL T_REQUIRE T_REQUIRE_ONCE
    %left ','
    @@ -874,6 +874,11 @@ variable_class_name:
    ;

    base_variable_with_function_calls:
    + raw_base_variable_with_function_calls { $$ = $1; }
    + | raw_base_variable_with_function_calls '[' dim_offset ']'
    { fetch_array_dim(&$$, &$1, &$3 TSRMLS_CC); }
    +;
    +
    +raw_base_variable_with_function_calls:
    base_variable { $$ = $1; }
    function_call {
    zend_do_begin_variable_parse(TSRMLS_C); $$ = $1; $$.u.EA.type =
    ZEND_PARSED_FUNCTION_CALL; }
    ;

    ------------------------------------------------------------------------

    [2009-10-26 18:45:24] melfar at gmail dot com

    Description:
    ------------
    PHP parser doesn't allow for array index operation to be used on a
    result of a function call. I propose a patch to allow such a
    construction.

    Reproduce code:
    ---------------
    <?php
    function car_brands() {
    return array("chevy", "hummer");
    }
    print car_brands()[1] . "\n";
    ?>


    Expected result:
    ----------------
    "hummer"



    Actual result:
    --------------
    Parse error: syntax error, unexpected '[' in test.php on line 5


    ------------------------------------------------------------------------


    --
    Edit this bug report at http://bugs.php.net/?id=50003&edit=1
  • Melfar at gmail dot com at Oct 26, 2009 at 10:13 pm
    ID: 50003
    User updated by: melfar at gmail dot com
    Reported By: melfar at gmail dot com
    -Status: Feedback
    +Status: Open
    Bug Type: Feature/Change Request
    Operating System: agnostic
    PHP Version: 5.3.0
    New Comment:

    How about this?

    <?php
    function car_brands() {
    return array("gm" => array("chevy" => 1, "hummer" => 2),
    "toyota" => array("prius" => 1, "corolla" => 2));
    }
    var_dump(car_brands()["toyota"]["corolla"]);
    ?>

    New patch is as follows:

    --- a/Zend/zend_language_parser.y.orig 2009-10-26 18:13:56.000000000
    +0300
    +++ b/Zend/zend_language_parser.y 2009-10-27 01:02:08.000000000
    +0300
    @@ -47,7 +47,7 @@
    %}

    %pure_parser
    -%expect 2
    +%expect 6

    %left T_INCLUDE T_INCLUDE_ONCE T_EVAL T_REQUIRE T_REQUIRE_ONCE
    %left ','
    @@ -874,6 +874,11 @@ variable_class_name:
    ;

    base_variable_with_function_calls:
    + raw_base_variable_with_function_calls { $$ = $1; }
    + | base_variable_with_function_calls '[' dim_offset ']' {
    fetch_array_dim(&$$, &$1, &$3 TSRMLS_CC); }
    +;
    +
    +raw_base_variable_with_function_calls:
    base_variable { $$ = $1; }
    function_call {
    zend_do_begin_variable_parse(TSRMLS_C); $$ = $1; $$.u.EA.type =
    ZEND_PARSED_FUNCTION_CALL; }
    ;


    Previous Comments:
    ------------------------------------------------------------------------

    [2009-10-26 20:58:42] johannes@php.net

    The idea was often discussed, but nobody found a fully satisfying
    solution, yet.

    Yours is limited to one array dimension.

    <?php
    function foo() {
    return array(array(42));
    }

    echo foo()[0][0];
    ?>

    Will for instance not work but should be supported.

    I also think it's likely to cause memory problems in edge cases while I
    wasn't able to create one in my quick tests.

    Anyidea aobut the above problem?

    ------------------------------------------------------------------------

    [2009-10-26 18:47:23] melfar at gmail dot com

    Not sure how to attach a file. The proposed patch is as follows.



    --- a/Zend/zend_language_parser.y.orig 2009-10-26 18:13:56.000000000
    +0300
    +++ b/Zend/zend_language_parser.y 2009-10-26 21:20:19.000000000
    +0300
    @@ -47,7 +47,7 @@
    %}

    %pure_parser
    -%expect 2
    +%expect 6

    %left T_INCLUDE T_INCLUDE_ONCE T_EVAL T_REQUIRE T_REQUIRE_ONCE
    %left ','
    @@ -874,6 +874,11 @@ variable_class_name:
    ;

    base_variable_with_function_calls:
    + raw_base_variable_with_function_calls { $$ = $1; }
    + | raw_base_variable_with_function_calls '[' dim_offset ']'
    { fetch_array_dim(&$$, &$1, &$3 TSRMLS_CC); }
    +;
    +
    +raw_base_variable_with_function_calls:
    base_variable { $$ = $1; }
    function_call {
    zend_do_begin_variable_parse(TSRMLS_C); $$ = $1; $$.u.EA.type =
    ZEND_PARSED_FUNCTION_CALL; }
    ;

    ------------------------------------------------------------------------

    [2009-10-26 18:45:24] melfar at gmail dot com

    Description:
    ------------
    PHP parser doesn't allow for array index operation to be used on a
    result of a function call. I propose a patch to allow such a
    construction.

    Reproduce code:
    ---------------
    <?php
    function car_brands() {
    return array("chevy", "hummer");
    }
    print car_brands()[1] . "\n";
    ?>


    Expected result:
    ----------------
    "hummer"



    Actual result:
    --------------
    Parse error: syntax error, unexpected '[' in test.php on line 5


    ------------------------------------------------------------------------


    --
    Edit this bug report at http://bugs.php.net/?id=50003&edit=1
  • Melfar at gmail dot com at Nov 7, 2009 at 11:20 pm
    ID: 50003
    User updated by: melfar at gmail dot com
    Reported By: melfar at gmail dot com
    Status: Open
    Bug Type: Feature/Change Request
    Operating System: agnostic
    PHP Version: 5.3.0
    New Comment:

    Actually, scratch the previous patch since it only worked on functions

    . Here is the fixed version, and it also prevents someone from
    modifying the temporary return value, as in:

    car_brands()["toyota"]["corolla"] = 4;

    The patch is as follows:

    --- a/Zend/zend_language_parser.y.orig 2009-10-26 18:13:56.000000000
    +0300
    +++ b/Zend/zend_language_parser.y 2009-11-08 02:06:20.000000000
    +0300
    @@ -47,7 +47,7 @@
    %}

    %pure_parser
    -%expect 2
    +%expect 7

    %left T_INCLUDE T_INCLUDE_ONCE T_EVAL T_REQUIRE T_REQUIRE_ONCE
    %left ','
    @@ -834,6 +834,11 @@
    ;

    variable:
    + raw_variable { $$ = $1; }
    + | variable '[' dim_offset ']' { fetch_array_dim(&$$, &$1,
    &$3 TSRMLS_CC); $$.u.EA.type = ZEND_PARSED_FUNCTION_CALL; }
    +;
    +
    +raw_variable:
    base_variable_with_function_calls T_OBJECT_OPERATOR {
    zend_do_push_object(&$1 TSRMLS_CC); }
    object_property { zend_do_push_object(&$4
    TSRMLS_CC); } method_or_not variable_properties
    { zend_do_pop_object(&$$ TSRMLS_CC);
    $$.u.EA.type = $1.u.EA.type | ($7.u.EA.type ? $7.u.EA.type :
    $6.u.EA.type); }


    Previous Comments:
    ------------------------------------------------------------------------

    [2009-10-26 22:13:18] melfar at gmail dot com

    How about this?

    <?php
    function car_brands() {
    return array("gm" => array("chevy" => 1, "hummer" => 2),
    "toyota" => array("prius" => 1, "corolla" => 2));
    }
    var_dump(car_brands()["toyota"]["corolla"]);
    ?>

    New patch is as follows:

    --- a/Zend/zend_language_parser.y.orig 2009-10-26 18:13:56.000000000
    +0300
    +++ b/Zend/zend_language_parser.y 2009-10-27 01:02:08.000000000
    +0300
    @@ -47,7 +47,7 @@
    %}

    %pure_parser
    -%expect 2
    +%expect 6

    %left T_INCLUDE T_INCLUDE_ONCE T_EVAL T_REQUIRE T_REQUIRE_ONCE
    %left ','
    @@ -874,6 +874,11 @@ variable_class_name:
    ;

    base_variable_with_function_calls:
    + raw_base_variable_with_function_calls { $$ = $1; }
    + | base_variable_with_function_calls '[' dim_offset ']' {
    fetch_array_dim(&$$, &$1, &$3 TSRMLS_CC); }
    +;
    +
    +raw_base_variable_with_function_calls:
    base_variable { $$ = $1; }
    function_call {
    zend_do_begin_variable_parse(TSRMLS_C); $$ = $1; $$.u.EA.type =
    ZEND_PARSED_FUNCTION_CALL; }
    ;

    ------------------------------------------------------------------------

    [2009-10-26 20:58:42] johannes@php.net

    The idea was often discussed, but nobody found a fully satisfying
    solution, yet.

    Yours is limited to one array dimension.

    <?php
    function foo() {
    return array(array(42));
    }

    echo foo()[0][0];
    ?>

    Will for instance not work but should be supported.

    I also think it's likely to cause memory problems in edge cases while I
    wasn't able to create one in my quick tests.

    Anyidea aobut the above problem?

    ------------------------------------------------------------------------

    [2009-10-26 18:47:23] melfar at gmail dot com

    Not sure how to attach a file. The proposed patch is as follows.



    --- a/Zend/zend_language_parser.y.orig 2009-10-26 18:13:56.000000000
    +0300
    +++ b/Zend/zend_language_parser.y 2009-10-26 21:20:19.000000000
    +0300
    @@ -47,7 +47,7 @@
    %}

    %pure_parser
    -%expect 2
    +%expect 6

    %left T_INCLUDE T_INCLUDE_ONCE T_EVAL T_REQUIRE T_REQUIRE_ONCE
    %left ','
    @@ -874,6 +874,11 @@ variable_class_name:
    ;

    base_variable_with_function_calls:
    + raw_base_variable_with_function_calls { $$ = $1; }
    + | raw_base_variable_with_function_calls '[' dim_offset ']'
    { fetch_array_dim(&$$, &$1, &$3 TSRMLS_CC); }
    +;
    +
    +raw_base_variable_with_function_calls:
    base_variable { $$ = $1; }
    function_call {
    zend_do_begin_variable_parse(TSRMLS_C); $$ = $1; $$.u.EA.type =
    ZEND_PARSED_FUNCTION_CALL; }
    ;

    ------------------------------------------------------------------------

    [2009-10-26 18:45:24] melfar at gmail dot com

    Description:
    ------------
    PHP parser doesn't allow for array index operation to be used on a
    result of a function call. I propose a patch to allow such a
    construction.

    Reproduce code:
    ---------------
    <?php
    function car_brands() {
    return array("chevy", "hummer");
    }
    print car_brands()[1] . "\n";
    ?>


    Expected result:
    ----------------
    "hummer"



    Actual result:
    --------------
    Parse error: syntax error, unexpected '[' in test.php on line 5


    ------------------------------------------------------------------------


    --
    Edit this bug report at http://bugs.php.net/?id=50003&edit=1
  • Jani at Nov 9, 2009 at 11:40 am
    ID: 50003
    Updated by: jani@php.net
    Reported By: melfar at gmail dot com
    -Status: Open
    +Status: Bogus
    Bug Type: Feature/Change Request
    Operating System: *
    PHP Version: 5.3.0
    New Comment:

    http://wiki.php.net/summits/pdmnotesmay09 (see Day 2, PHP 6, #13).



    Previous Comments:
    ------------------------------------------------------------------------

    [2009-11-07 23:19:56] melfar at gmail dot com

    Actually, scratch the previous patch since it only worked on functions

    . Here is the fixed version, and it also prevents someone from
    modifying the temporary return value, as in:

    car_brands()["toyota"]["corolla"] = 4;

    The patch is as follows:

    --- a/Zend/zend_language_parser.y.orig 2009-10-26 18:13:56.000000000
    +0300
    +++ b/Zend/zend_language_parser.y 2009-11-08 02:06:20.000000000
    +0300
    @@ -47,7 +47,7 @@
    %}

    %pure_parser
    -%expect 2
    +%expect 7

    %left T_INCLUDE T_INCLUDE_ONCE T_EVAL T_REQUIRE T_REQUIRE_ONCE
    %left ','
    @@ -834,6 +834,11 @@
    ;

    variable:
    + raw_variable { $$ = $1; }
    + | variable '[' dim_offset ']' { fetch_array_dim(&$$, &$1,
    &$3 TSRMLS_CC); $$.u.EA.type = ZEND_PARSED_FUNCTION_CALL; }
    +;
    +
    +raw_variable:
    base_variable_with_function_calls T_OBJECT_OPERATOR {
    zend_do_push_object(&$1 TSRMLS_CC); }
    object_property { zend_do_push_object(&$4
    TSRMLS_CC); } method_or_not variable_properties
    { zend_do_pop_object(&$$ TSRMLS_CC);
    $$.u.EA.type = $1.u.EA.type | ($7.u.EA.type ? $7.u.EA.type :
    $6.u.EA.type); }

    ------------------------------------------------------------------------

    [2009-10-26 22:13:18] melfar at gmail dot com

    How about this?

    <?php
    function car_brands() {
    return array("gm" => array("chevy" => 1, "hummer" => 2),
    "toyota" => array("prius" => 1, "corolla" => 2));
    }
    var_dump(car_brands()["toyota"]["corolla"]);
    ?>

    New patch is as follows:

    --- a/Zend/zend_language_parser.y.orig 2009-10-26 18:13:56.000000000
    +0300
    +++ b/Zend/zend_language_parser.y 2009-10-27 01:02:08.000000000
    +0300
    @@ -47,7 +47,7 @@
    %}

    %pure_parser
    -%expect 2
    +%expect 6

    %left T_INCLUDE T_INCLUDE_ONCE T_EVAL T_REQUIRE T_REQUIRE_ONCE
    %left ','
    @@ -874,6 +874,11 @@ variable_class_name:
    ;

    base_variable_with_function_calls:
    + raw_base_variable_with_function_calls { $$ = $1; }
    + | base_variable_with_function_calls '[' dim_offset ']' {
    fetch_array_dim(&$$, &$1, &$3 TSRMLS_CC); }
    +;
    +
    +raw_base_variable_with_function_calls:
    base_variable { $$ = $1; }
    function_call {
    zend_do_begin_variable_parse(TSRMLS_C); $$ = $1; $$.u.EA.type =
    ZEND_PARSED_FUNCTION_CALL; }
    ;

    ------------------------------------------------------------------------

    [2009-10-26 20:58:42] johannes@php.net

    The idea was often discussed, but nobody found a fully satisfying
    solution, yet.

    Yours is limited to one array dimension.

    <?php
    function foo() {
    return array(array(42));
    }

    echo foo()[0][0];
    ?>

    Will for instance not work but should be supported.

    I also think it's likely to cause memory problems in edge cases while I
    wasn't able to create one in my quick tests.

    Anyidea aobut the above problem?

    ------------------------------------------------------------------------

    [2009-10-26 18:47:23] melfar at gmail dot com

    Not sure how to attach a file. The proposed patch is as follows.



    --- a/Zend/zend_language_parser.y.orig 2009-10-26 18:13:56.000000000
    +0300
    +++ b/Zend/zend_language_parser.y 2009-10-26 21:20:19.000000000
    +0300
    @@ -47,7 +47,7 @@
    %}

    %pure_parser
    -%expect 2
    +%expect 6

    %left T_INCLUDE T_INCLUDE_ONCE T_EVAL T_REQUIRE T_REQUIRE_ONCE
    %left ','
    @@ -874,6 +874,11 @@ variable_class_name:
    ;

    base_variable_with_function_calls:
    + raw_base_variable_with_function_calls { $$ = $1; }
    + | raw_base_variable_with_function_calls '[' dim_offset ']'
    { fetch_array_dim(&$$, &$1, &$3 TSRMLS_CC); }
    +;
    +
    +raw_base_variable_with_function_calls:
    base_variable { $$ = $1; }
    function_call {
    zend_do_begin_variable_parse(TSRMLS_C); $$ = $1; $$.u.EA.type =
    ZEND_PARSED_FUNCTION_CALL; }
    ;

    ------------------------------------------------------------------------

    [2009-10-26 18:45:24] melfar at gmail dot com

    Description:
    ------------
    PHP parser doesn't allow for array index operation to be used on a
    result of a function call. I propose a patch to allow such a
    construction.

    Reproduce code:
    ---------------
    <?php
    function car_brands() {
    return array("chevy", "hummer");
    }
    print car_brands()[1] . "\n";
    ?>


    Expected result:
    ----------------
    "hummer"



    Actual result:
    --------------
    Parse error: syntax error, unexpected '[' in test.php on line 5


    ------------------------------------------------------------------------


    --
    Edit this bug report at http://bugs.php.net/?id=50003&edit=1

Related Discussions

Discussion Navigation
viewthread | post