2011-11-29 31 views
0

我想要做的是将静态$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 
+0

,你能告诉我们哪些第8行是? – Landon

回答

2

:中

self::$variable = 'new value'; 

代替:

static::variable = 'new value' 

BTW我强烈建议您使用IDE能够直接告诉你基本的语法错误,l ike Aptana StudioPHPStorm

1

你缺少美元符号和一个冒号,并应使用self引用数据成员$variable

self::$variable = 'new value'; 
1

你需要使用self

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