Strange parse error with static concatenated strin

2019-06-21 04:58发布

I'm getting this error:

Parse error: syntax error, unexpected '.', expecting ',' or ';' in /var/(...)/config.php on line 5

With this (simplified) code:

<?php

class Config
{
   public static $somevar = "Date: " . date('Y');
}

?>

I thought this was valid php, but I guess not... what am I doing wrong here? Thanks!

3条回答
等我变得足够好
2楼-- · 2019-06-21 05:31

According to the PHP docs:

Like any other PHP static variable, static properties may only be initialized using a literal or constant; expressions are not allowed. So while you may initialize a static property to an integer or array (for instance), you may not initialize it to another variable, to a function return value, or to an object.

Try writing

Config::$somevar = "Date: " . date('Y');

after the class definition.

查看更多
The star\"
3楼-- · 2019-06-21 05:37

No operation or function is allow for property initialization, because this is evaluated when parsing.

查看更多
叛逆
4楼-- · 2019-06-21 05:41

From Manual

Like any other PHP static variable, static properties may only be initialized using a literal or constant; expressions are not allowed. So while you may initialize a static property to an integer or array (for instance), you may not initialize it to another variable, to a function return value, or to an object.

查看更多
登录 后发表回答