phpparse-errorphp-parse-error

PHP Parse error using DIRECTORY_SEPARATOR in class variable


Why can I assign

$tmpPath = DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR;

within a class method but get an PHP Parse error when I trie to assign it to a protected class variable like this:

protected $_tmpPath = DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR;

Here is the error log:

PHP Parse error: syntax error, unexpected '.', expecting ',' or ';' in ...


Solution

  • Property values must be a single, constant value, not an expression.

    Unless you're using PHP5.6 in which case the code you have there is perfectly allowed.

    But until you do, the typical workaround is to assign the value to it in the class's constructor.