PHP use return value of function as array

Why it works:

        $cacheMatchesNotPlayed = $cache->load('externalData');
        $cacheMatchesNotPlayed = $cacheMatchesNotPlayed['matchesNotPlayed'];

      

But this doesn't work:

        $cacheMatchesNotPlayed = $cache->load('externalData')['matchesNotPlayed'];

      

Is there any reason for this? The second bit is easier to write.

+2


a source to share


3 answers


PHP does not support the function array unordering you want to do.

See the RFC on the topic http://wiki.php.net/rfc/functionarraydereferencing and related mailings that were rejected last year around this time. While there has been support for this, and it's still a common query (usually "why doesn't this work?"), There are no plans to introduce this syntax.

As for why, it is just that the code that implements it has not been submitted for approval (if it has been written at all).



Update

This feature is implemented in the trunk (main development) of PHP and will probably be included in the next version without a fix (5.4.0). For those who want to play with FAD, feel free to upload an outside line snapshot .

+6


a source


As PHP doesn't support this syntax.



+7


a source


PHP simply doesn't support array dereferencing the way you describe it. Also here:

PHP syntax for the result of a dereference function

+5


a source







All Articles