Re: Suggestion: echo function(var)[0];

From: Date: Mon, 07 Jun 2010 15:01:30 +0000
Subject: Re: Suggestion: echo function(var)[0];
References: 1 2 3 4 5  Groups: php.internals 
Request: Send a blank email to internals+get-48670@lists.php.net to get a copy of this message
Hi all, 2010/6/5 Felipe Pena <felipensp@gmail.com> > Hi! > > 2010/6/4 Stas Malyshev <smalyshev@sugarcrm.com> > > Hi! >> >> >> function call chaining (f()() if f() returns function), and array >>> dereferencing (f()[0]) - (Stas) >>> >> >> I did patch for f()() - it's referenced at >> http://wiki.php.net/rfc/fcallfcall - but not for f()[] - >> didn't have time >> for that yet. >> >> It should not be too hard to do, one just has to be careful with refcounts >> so that the returned result could be freed properly and without hurting the >> referred element. >> > > I wrote a patch, but I cannot test all cases, so I'm not sure if is okay at > all. > > What about this approach: > http://felipe.ath.cx/diff/array_dereference.diff? > > I've updated the patch, now it works with method call as well. Example: <?php class foo { public $x = 10; public function bar() { $x = array(); $x[] = 3; $x[] = array(1, 5); $x[] = new foo; return $x; } } $foo = new foo; var_dump($foo->bar()[2]->bar()[0]); // 3 var_dump($foo->bar()[2]->bar()[1]); // array(0 => 1, 1 => 5) var_dump($foo->bar()[2]->bar()[1][1]); // 5 var_dump($foo->bar()[2]->bar()[2]->x); // 10 function test($arr) { $x = $arr; return $x; } $array = array(1, new foo); var_dump(test($array)[0]); // 1 var_dump(test($array)[1]->bar()[0]); // 3 var_dump(test($array)[1]->bar()[2]); // object(foo) http://felipe.ath.cx/diff/array_dereference.diff Thanks. -- Regards, Felipe Pena

« previous php.internals (#48670) next »