Search code examples
phpstatic-variables

Can't use a variable inside static variable definition?


static $PATH_TO_USER = $server . '/users';

I'm getting a syntax error. If I remove the static, it accepts it, though.

It's not a big deal to type the whole thing out, but I'm not sure why this isn't working in the first place


Solution

  • Static variable

    Static variables may be declared as seen in the examples above. Trying to assign values to these variables which are the result of expressions will cause a parse error.

    via the PHP Manual.

    Static property

    Like any other PHP static variable, static properties may only be initialized using a literal or constant; expressions are not allowed.

    via the PHP Manual.