我想做的是将静态$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 Studio或PHPStorm。您缺少美元符号和冒号,应该使用self
来引用数据成员$variable
:
self::$variable = 'new value';
您需要使用self
:
class myobj {
static protected $variable = null;
static function test() {
self::$variable = 'new value';
}
}