20 见识__get与__set的真实用例
代码
record(['name'=>'后盾人','age'=>21,'tel'=>'19999999999']);
}
}
class Model extends Query
{
protected $field=[];
public function all()
{
$this->select();
return $this->field;
}
protected function record(array $data)
{
$this->field=$data;
}
protected function __tel()
{
return substr($this->field['tel'],0,8).'***';
}
public function __get($name)
{
//echo $name;
//die;
if(method_exists($this,'__'.$name)){
return call_user_func_array([$this,'__'.$name],[]);
}
if(isset($this->field[$name])){
return $this->field[$name];
}
throw new Exception('arg error!');
}
public function __set($name,$value){
if(isset($this->field[$name])){
$this->field[$name]=$value;
}
else{
throw new Exception('arg error!');
}
}
}
try{
$user=new Model;
//print_r($user->all());
//echo '
';
$user->all();
$user->name='xuzwlyy';
//echo $user->tel;
echo $user->name;
}catch(Exception $e){
echo $e->getMessage();
}