动态属性问题给程序带来很多麻烦,以下是一位朋友的解决方法如下:
From : http://bbs.php100.com/read-htm-tid-25526.html
phpclass testClass {private $va;private $vb;private $vc;private $allVars;public function __construct(){echo 'testClass __construct'.'';//$this->va = 1;$this->getAllVars();}public function __GET($name) {return $this->$name;}public function __SET($name, $value) {foreach($this->allVars as $key) {if ($name == "$key") {$this->$name = $value;return;}}throw new exception ('你访问了未定义的属性');}private function getAllProps() {$r = new ReflectionClass($this);return ($r->getProperties());}private function getAllVars() {$classProps = $this->getAllProps();foreach($classProps as $key){$temparr = get_object_vars($key);$this->allVars[] .= $temparr['name'];}}
}$rc = new testClass();
$rc->va = 5;
$rc->vb = 5;
$rc->vc = 5;
$rc->vd = 6;
?>