News for hackers. Stuff that matters.
Re:What PHP 5.5 might look like by mikeash on Thursday July 12, 2012 @10:14AM (#4234573)
Regarding the "constant dereferencing" proposal, how do you even make a language parser that can't apply array operations to literals in the first place? I'm trying very hard not to bash on PHP here, and this is a completely honest question. I can't figure out how you'd even go about breaking that if you wanted to. Anyone know the background there?
Re:What PHP 5.5 might look like by sanxiyn on Thursday July 12, 2012 @11:10AM (#4234874)
As I understand, it goes like this: in PHP grammar, expressions are divided into two classes: variables, and expressions without variables. To simplify, you can think variables start with $, expressions without variables don't. These two classes are handled in completely different ways. Since array literals are expressions without variables, it isn't too strange that $x[0] parses, but [0][0] doesn't.

One reason it is like that has to do with string interpolation. PHP interpolates $x[0] inside double quotes, but does not (and probably should not) interpolate [0][0]. I am not saying this is a good solution, but hopefully that answers "how is that even possible?" question.

Re:What PHP 5.5 might look like by pbiggar on Thursday July 12, 2012 @11:10AM (#4234872)
This would probably ban "4[5]" but not "$x = 4; $x[5]".

PHP enforces a lot of things in the parser (as opposed to making bytecode and having a simple type checker). Instead of having a general "expression" type which can be dereferenced, they have different rules for scalars, strings, variables, function calls, etc. For a long time, you couldn't dereference the result of a function call, because the parser didn't allow it, so this is probably the same.

Re:What PHP 5.5 might look like by mikeash on Thursday July 12, 2012 @12:55PM (#4235436)
Enforcing it in the parser seems so horribly wrong. If you want to disallow stuff like 4[5] at compile time then you should do that in a separate pass on the AST, not in the parser itself. That's ought to be a semantic error, not a syntax error. Treating that separately would require all manner of special cases. But, I guess that must be what they do.
Re:What PHP 5.5 might look like by pbiggar on Thursday July 12, 2012 @01:02PM (#4235465)
Yeah, there's tons of special cases in the parser. I worked on phc (http://phpcompiler.org), so I've read the PHP parser and helped write some of our own. We took the AST approach and it worked out pretty well. But PHP was written before its authors knew good interpreter design, and its currently very hard to refactor it out to a better design.
Re:What PHP 5.5 might look like by mikeash on Thursday July 12, 2012 @02:19PM (#4235853)
Painful. Thanks for sharing your experience. Cool to ask such a question and get an answer from someone who's actually built something related.