php _get _set,该如何解决

WBOY
Release: 2016-06-13 10:42:40
Original
846 people have browsed it

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template