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.


class MyClass
    public function giveMeArray()
        return array('test'=>'myvalue');

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

$myClass = new MyClass(); 
$result = $myClass->giveMeArray(); 
echo $result['test'];

In the new PHP version you can do just:

$myClass = new MyClass(); 
echo $myClass->giveMeArray()['test'];


One thought on “Array dereferencing support in PHP 5.4.0”

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.