请向我解释设置静态变量时出现此错误的性质


please explain to me nature of this error showing up when setting static variable

我想做的是将静态$variable设置为'new value'。这是我的代码:

class myobj {
    static protected $variable = null;
    static function test() {
        static::variable = 'new value'
    }
}
$obj = new myobj();
$obj->test();

但是显示错误:

Parse error: syntax error, unexpected '=' in D:'!TC'www'error.php on line 8

使用说明:

self::$variable = 'new value';

代替:

static::variable = 'new value'
我强烈建议您使用能够直接告诉您基本语法错误的IDE,如Aptana StudioPHPStorm

您缺少美元符号和冒号,应该使用self来引用数据成员$variable:

self::$variable = 'new value';

您需要使用self:

class myobj {
  static protected $variable = null;
  static function test() {
    self::$variable = 'new value';
  }
}