php5类中的__get()和__set()方法用于private私有属性

来源:互联网 发布:淘宝买的模板在哪里 编辑:程序博客网 时间:2024/05/22 15:10


class B{private $s1;function __set($p, $v){$this->$p = $v.'---';}function __get($p){if(isset($p)){return $this->$p;}else {    $this->$p = null;}return $this->$p;}function test(){echo $this->s1;}}$b = new B();$b->s1 = '======================';echo $b->s1;exit;

因为这2个方法只适用于private私有,所以在一些特殊场合还是很好用的!!

并且在赋值的时候,可以在方法里做一些特殊的判断。


0 0