我知道我是很晚才到这里聚会,但我张贴此为edificationof任谁也碰到这个问题绊倒。首先,我认为测试类是错误的,如果这真的是第三方组件的功能,那么我会挑选它,因为它是垃圾。你真的想要所有的属性名称内部映射到单个属性的'名称',从而相互覆盖?你真的希望所有的属性名称都作为属性值返回吗?该代码应该是这样的:
class test {
public function __get($name){
return $this->$name;
}
public function __set($name,$value){
$this->$name = $value;
}
}
其次,你可以改变类的,即使它有保持不变。这是继承的关键。这是open-closed principle。如果函数是不正确的,只是延长测试这样的改正:
class test {
private $name;
public function __get($name){
return $name;
}
public function __set($name,$value){
$this->name = $value;
}
}
class my_test extends test
{
public function __get($name)
{
return $this->$name;
}
public function __set($name,$value){
$this->$name = $value;
}
}
你不应该需要定义__isset()
为更正后的代码会做这是什么意思的事,但如果你这样做,你可以做这也是。
现在,下面将做什么是应该做的(注意类名称的变化):)`
$obj = new my_test();
$obj->a = 2;
if (!empty($obj->a)) {
echo 'not empty';
}
为什么不定义'__isset(? – 2010-09-17 12:32:25
我做了一个更新,不能改班。 – danidacar 2010-09-17 13:39:11