我似乎不明白为什么下面的代码只打印“TEST”两次.
class A {
private $test = "TEST
";
public static function getInstance() {
return new self();
}
public static function someStaticMethod() {
$a = new self();
$a->test;
}
public function __get($args) {
echo $this->$args;
}
}
/* echo's "TEST" */
$a = new A();
$a->test;
/* echo's "TEST" */
$a2 = A::getInstance();
$a2->test;
/*
No output... eeerhm... how come?
Why is $a->test (inside someStaticMethod()) not being overloaded by __get ??
*/
A::someStaticMethod();
?>
PHP网站说(link):
Property overloading only works in object context. These magic methods will not be triggered in static context. Therefore these methods should not be declared static. As of PHP 5.3.0, a warning is issued if one of the magic overloading methods is declared static.
但我认为他们试图说你应该宣布魔术方法是静态的.例如.:
public static function __get(){}
事实上,我实际上是在对象上下文中使用它. $a = new self();返回变量$a中A类的实例.然后我使用$a-> test(对象上下文imo?)来获取私有的“test”变量,而该变量又应该被重载…
我很迷惑…