Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP use function return value as array

Tags:

php

Why is it that this works:

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

But this doesn't work:

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

Is there some reason for it? The second bit is easier to write.

like image 501
Richard Knop Avatar asked Jun 24 '26 10:06

Richard Knop


2 Answers

Because PHP does not support that syntax.

like image 105
Tyler Carter Avatar answered Jun 30 '26 07:06

Tyler Carter


PHP does not support the function array dereferencing, which you want to do.

See an RFC on the subject http://wiki.php.net/rfc/functionarraydereferencing, and the associated mailing list conversations, which was declined roughly this time last year. While there was support for this, and it's still a common request (usually "why doesn't this work?"), there are no plans in motion to introduce this syntax.

As for why, quite simply the code implementing it has not been submitted for approval (if it has been written at all).

Update

This feature has been implemented in the trunk (main development) branch of PHP and will likely be included in the next non-bugfix version (5.4.0). For anyone wanting to play with FAD, feel free to download a trunk snapshot.

like image 43
salathe Avatar answered Jun 30 '26 08:06

salathe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!