当调用或者设置类不存在的方法时,_会调用_set和__get方法
<?php
class HandsonBoy
{
private $name = ‘chenqionghe‘;
private $age = 18;
public function __set($name,$value)
{
echo ‘您正在设置私有属性‘.$name.‘<br >值为‘.$value.‘<br>‘;
$this->$name = $value;
}
public function __get($name)
{
if(!isset($this->$name))
{
echo ‘未设置‘.$name;
$this->$name = "正在为你设置默认值".‘<br>‘;
}
return $this->$name;
}
}
$a = new HandsonBoy();
echo $a->name.‘<br />‘;
$a->hair = ‘short‘;
原文:http://www.cnblogs.com/chenqionghe/p/4735698.html