Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

list() Unpacking in foreach Declaration #60

Open
WyrdNexus opened this issue Sep 18, 2014 · 1 comment
Open

list() Unpacking in foreach Declaration #60

WyrdNexus opened this issue Sep 18, 2014 · 1 comment

Comments

@WyrdNexus
Copy link

list() Unpacking in foreach Declaration Affects Syntax Error

PHP Feature added in 5.5, so likely simply hasn't been reviewed yet.
http://php.net/manual/en/control-structures.foreach.php#control-structures.foreach.list

foreach unpacking with list()

$array = new array(
    "X" => ['one',2,3],
    "Y" => ['two',4,5]
);

// case 1:
foreach( $array as list($a, $b )) {  // Syntax error, unexpected T_LIST
    echo "\n$a $b";
}

// case 2:
foreach( $array as $key => list($a, $b, $c) ) {  // Syntax error, unexpected T_LIST
    echo "\n$key: $a $b - $c";
}

Output:

one 2
two 4
X: one 2 - 3
Y: two 4 - 5

@WyrdNexus
Copy link
Author

Perhaps adding 2 entries to grammar/zend_language_parser.jsy (192):

| T_FOREACH '(' expr T_AS T_LIST ')' foreach_statement
          { $$ = Stmt_Foreach[$3, $5, [keyVar: null, byRef: false, stmts: $7]]; }
| T_FOREACH '(' expr T_AS variable T_DOUBLE_ARROW T_LIST ')' foreach_statement
          { $$ = Stmt_Foreach[$3, $6, [keyVar: $5, byRef: $7, stmts: $8]]; }

... i'm just guessing here, but I'll play with it if I have time to review all of your Rep, and you don't get to it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant