Home  >  Article  >  Backend Development  >  php _get _set,该如何解决

php _get _set,该如何解决

WBOY
WBOYOriginal
2016-06-13 13:13:01779browse

php _get _set
求大神指教以下代码的输出,为什么我得到的总是1,和2
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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn