Hi all,

2010/6/5 Felipe Pena <felipe...@gmail.com>

> Hi!
>
> 2010/6/4 Stas Malyshev <smalys...@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

Reply via email to