2

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.

0

3 Answers 3

7

Because PHP does not support that syntax.

Sign up to request clarification or add additional context in comments.

3 Comments

@Andrey: I can think of far stupider limitations besides this. :-)
@salathe - i said one of :)
@Andrey: haha, fair enough :)
6

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.

2 Comments

Is it difficult to implement this functionality or is there some other reason for this? Because it's a really annoying "feature" of PHP, I always use it that way and then wonder why my code is not working.
I've never looked into what would be necessary to implement it, so I can't really say how difficult it would be. All I can say is that there have been voices raised on the subject for many years, but still no code. Though perhaps the complexity of the task and lack of patches submitted bear no real correlation.
5

PHP just does not support array dereferencing in the way you have described. Also covered here:

PHP syntax for dereferencing function result

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.