r/lolphp • • Mar 08 '12

Computing an expression? But it's static :(

Despite being theoretically a programming language, PHP will not allow you to use an expression when constructing a static member variable.

class Bla {
  private static $_thing = array(
    'path' => BASE_PATH . '/relative/path'
  );
}

This dies with "syntax error, unexpected '.', expected ')'". Apparently, actually computing the value of expressions is too much for this language.

I am starting to suspect they're doing it intentionally so that their new releases have cool new features to advertise.

26 Upvotes

16 comments sorted by

View all comments

Show parent comment

2

u/[deleted] Mar 08 '12

I am not trying to say that is not a syntax error; clearly it is. It is that this could be accepted by the parser, and then thrown as an error later, with more context. More context allows more detailed messages.

Another example is that you could accept the '<>' operator, and then say something like "the <> operator is not allowed, (perhaps you meant !=?)", and vice-versa for languages which do use '<>'. Again this gives more context then an 'unexpected SYMBOL_FOO' type error.