php _get _set
求大神指教以下代码的输出,为什么我得到的总是1,和2
<?php
class myClass{
private $arr = array('x'=>null,'y'=>null);
function _get($property){
if(array_key_exists($property,$this->arr)){
return $this->arr[$property];
}else{
echo "can not find key $property";
}
}
function _set($property,$value){
if(array_key_exists($property,$this->arr)){
$this->arr[$property] = $value;
}else{
echo "can not find key $property";
}
}
}
$obj = new myClass();
$obj->x = 1;
echo $obj->x;
print "\n";
$obj->d = 2;
echo $obj->d;
?>
[解决办法]
魔术方法前面都是双下划线 __get、__set