数学符号不';不能在PHP类定义中工作?

数学符号不';不能在PHP类定义中工作?,php,syntax-error,Php,Syntax Error,我收到错误信息: 消息:语法错误,意外的“*”,应为“,”或“;” 当我尝试使用数学符号在类中定义变量时。比如说, class Foo { public static $test = 1+1; } echo Foo::$test; 产生这个错误。是否可以在PHP的类变量定义中使用math?表达式不允许作为字段默认值。你可以试试这个 class Foo { public static $test; public function __construct(){

我收到错误信息:

消息:语法错误,意外的“*”,应为“,”或“;”

当我尝试使用数学符号在类中定义变量时。比如说,

class Foo {
    public static $test = 1+1;
}

echo Foo::$test;

产生这个错误。是否可以在PHP的类变量定义中使用math?

表达式不允许作为字段默认值。你可以试试这个

class Foo {
    public static $test;

    public function __construct(){
        $this->test = 1+1;
    }
}

echo Foo::$test;

不,你必须在构造函数中这样做。我明白了,所以我应该使用构造函数。解释在文档中-是或在getter和setter中