php 的簡單存在 (魔術方法作用之一:容錯)
name =$name;
$this->age =$age;
}
//獲取魔術方法
# @param string $name
public function __get($param){
// return $this->$param;
#允許被訪問的屬性
$allow = array('age');
if(in_array($param, $allow)){
return $this->$param;
}
}
//__set() 設置魔術方法
#@param string $name
#param string $val
public function __set($name,$val){
$allow = array('age','money');
if(in_array($name, $allow)){
$this->$name=$val;
}
}
// __isset() 判定數據
public function __isset($name){
return isset($this->$name);
}
//__unset 銷毀數據
public function __unset($name){
//建立unset列表
$allow =array('tail');
if(in_array($name,$allow)){
echo 'unset running';
unset($this->$name);
}
}
}
//實例化
$p = new Person('zp',21);
$p->age = 200;
echo $p->money = 200000000;
$p->tail ="a";
var_dump(isset($p->name));
var_dump(isset($p->age));
var_dump(isset($money));
// var_dump(empty($tail));
unset($p->tail);
// var_dump($tail);