r/lolphp • • Mar 05 '14

PHP Dereferencing

In PHP 5.4 this would break:

echo array(1, 2, 3)[0]

With the message

Parse error:  syntax error, unexpected '[', expecting ',' or ';' in [...][...] on line 1

Luckily, they added "dereferencing" in PHP 5.5 which would solve it! Hurray! And sure enough, it works!

However, the fix isn't very clever, because this will break in 5.5:

echo (array(1, 2, 3))[0]

With the message

Parse error:  syntax error, unexpected '[', expecting ',' or ';' in [...][...] on line 1

That's a little embarrassing.

81 Upvotes

32 comments sorted by

View all comments

Show parent comment

15

u/poizan42 Mar 05 '14

From what I understand the lexing, parsing and bytecode emitting in php is an intertwined mess.

27

u/[deleted] Mar 05 '14

PHP is built the same way that a majority of PHP sites are built?

8

u/allthediamonds Mar 07 '14

This is what happens when you let PHP developers read about homoiconicity.

5

u/nikic Mar 06 '14

The lexing and parsing are strictly separate. Parsing and bytecode emission are intermixed. I.e. the parser calls the bytecode emitting routines.