#50003 [Opn->Fbk]: Allow constructions like func()[0]

From: Date: Mon, 26 Oct 2009 20:58:43 +0000
Subject: #50003 [Opn->Fbk]: Allow constructions like func()[0]
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-143974@lists.php.net to get a copy of this message
 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



Thread (6 messages)

« previous php.bugs (#143974) next »