开发者

make "$arr = Foo::bar(); $v = $arr[$i]" into one line of code

I run into a special situation that开发者_JS百科 needs to make

$arr = Foo::bar();
$v = $arr[$i]

into one line of code. $v = Foo::bar()[$i] is not working.


That's not possible with PHP at the minute, although I do believe it's being added for the next PHP version. Here's some more information on the subject:

http://schlueters.de/blog/archives/138-Features-in-PHP-trunk-Array-dereferencing.html

I can't wait for this to be added too.


How's this? Exactly what you wanted? =P

$v = Foo::barElement(0);

and...

class Foo
{
    public static function barElement($index) {$t = self::bar(); return $t[$index];}

    public static function bar......
}


$v = reset(Foo::bar());


Array derefencing is not yet implemented in the current versions of PHP. You'll have to wait until PHP 5.4 or 6.0 are released.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜