具有静态串联字符串变量的奇怪解析错误
我遇到此错误:
解析错误:语法错误,意外的'。',期望为','或';在第5行的/var/(....../config.php)中
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');
}
?>
我认为这是有效的php,但我想不是……我在这里做错什么了吗?谢谢!
I thought this was valid php, but I guess not... what am I doing wrong here? Thanks!
根据 PHP文档:
就像其他任何PHP静态变量一样,静态属性可能仅使用文字或常量进行初始化;不允许使用表达式。因此,尽管您可以将静态属性初始化为整数或数组(例如),但不能将其初始化为另一个变量,函数返回值或对象。
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.
尝试写作
Config::$somevar = "Date: " . date('Y');
在类定义之后。