OBS:我直接在这里编码,因为我的代码要复杂得多。
如果我编码:
class SuperFoo {
public function __get($name) {
return $this->$name;
}
public function __set($name, $value) {
$this->$name = $value;
}
}
class Foo extends SuperFoo {
private $bar = '';
}
$foo = new Foo();
$foo->bar = "Why it doesn't work?";
var_dump($foo);
结果是:
object(Foo) {
["bar":"Foo":private]=>
string(0) ''
}
而不是:
object(Foo) {
["bar":"Foo":private]=>
string(20) 'Why it doesn't work?'
}
为什么会这样?我不想使用数组来保存属性,因为我需要将它们声明为单独的私有成员。