PHP不可访问属性get() set() isset() unset()
时间:6年前 阅读:4655
get()
读取不可访问属性(protected、private)时,__get()会被调用,并将属性名以第一个参数(string)传进此方法中
public mixed __get ( string $name )
<?php class demo{ protected $protected = 1; public $public = 2; private $private = 3; function __get($name){ echo "111{$name}111<br>"; } } $d1 = new demo; $d1->protected;//111protected111 $d1->public; $d1->private;//111private111 ?>
set()
在给不可访问属性(protected、private)赋值时,__set() 会被调用,并将属性名以第一个参数(string),值作为第二参数(mixed)传进此方法中
public void __set ( string $name , mixed $value )
<?php class demo{ protected $protected = 1; public $public = 2; private $private = 3; function __set($name,$value){ echo "0{$name}0{$value}<br>"; } } $d1 = new demo; $d1->protected = '1';//0protected01 $d1->public = '2'; $d1->private = '3';//0private03 ?>
isset()
当对不可访问属性(protected、private)调用 isset() 或 empty() 时,__isset() 会被调用
public bool __isset ( string $name )
<?php class demo{ protected $protected = 1; public $public = 2; private $private = 3; function __isset($name){ echo "0{$name}0<br>"; } } $d1 = new demo; empty($d1->protected);//0protected0 empty($d1->public); empty($d1->private);//0private0 ?>
unset()
当对不可访问属性(protected、private)调用unset()时,__unset()会被调用
public void __unset ( string $name )
<?php class demo{ protected $protected = 1; public $public = 2; private $private = 3; function __unset($name){ echo "0{$name}0<br>"; } } $d1 = new demo; unset($d1->protected);//0protected0 unset($d1->public); unset($d1->private);//0private0 ?>
本站声明:网站内容来源于网络,如有侵权,请联系我们https://www.qiquanji.com,我们将及时处理。
微信扫码关注
更新实时通知
网友评论