Array dereferencing support in PHP 5.4.0

A new nice improvement in PHP 5.4 As you can see in the news list they added array dereferencing support. What this actually means?

If a method of your class returns an array, then to use a specific element you will need another variable.
Example:
[sourcecode language=”php”]
class MyClass
{
public function giveMeArray()
{
return array(‘test’=>’myvalue’);
}
}
[/sourcecode]

If you want to display ‘myvalue’, you should do something like

[sourcecode language=”php”]
$myClass = new MyClass();
$result = $myClass->giveMeArray();
echo $result[‘test’];
[/sourcecode]

In the new PHP version you can do just:

[sourcecode language=”php”]
$myClass = new MyClass();
echo $myClass->giveMeArray()[‘test’];
[/sourcecode]

2 comments

Leave a Reply to Alejandro Morales Cancel reply

Your email address will not be published. Required fields are marked *