PHP 获取不可见的属性自动调用的函数 `__get`

如果获取->属性 php会自动调用这个方法

Python
<?php class Stuendent{ private $name; public function __set($name,$value){ if (property_exists($this, $name)) { $this->$name = $value; }else { throw new \Exception("没有这个私有变量属性", 1); } } public function __get($name){ return $this->$name."__get"; } } $st = new Stuendent; // 实例化 $st->name = "songhao"; print_r($st); echo $st->name;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
< ? php
class Stuendent {
     private $ name ;
 
     public function __set ( $ name , $ value ) {
         if ( property_exists ( $ this , $ name ) ) {
             $ this -> $ name = $ value ;
         } else {
             throw new \ Exception ( "没有这个私有变量属性" , 1 ) ;
 
         }
 
     }
     public function __get ( $ name ) {
         return $ this -> $ name . "__get" ;
     }
}
 
$ st = new Stuendent ;
/ / 实例化
$ st -> name = "songhao" ;
print_r ( $ st ) ;
 
echo $ st -> name ;
 
 

你如果不想用 __get 获取所有类的属性

Python
<?php class Stuendent{ private $white_array = ['<span class="wp_keywordlink"><a href="http://www.168seo.cn/python" title="python">python</a></span>']; private $name; private $<span class="wp_keywordlink"><a href="http://www.168seo.cn/python" title="python">python</a></span>='I can write Python code'; public function __set($name,$value){ if (property_exists($this, $name)) { $this->$name = $value; }else { throw new \Exception("没有这个私有变量属性", 1); } } public function __get($name){ if(in_array($name, $this->white_array)){ // 检测这个属性值 是否在white_array这个属性里面 // 如果在 则返回这个值,如果不在就返回null return $this->$name."__get"; }else { return null; } } } $st = new Stuendent; // 实例化 $st->name = "songhao"; echo $st->name; // 无输出 echo $st-><span class="wp_keywordlink"><a href="http://www.168seo.cn/python" title="python">python</a></span>;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
< ? php
class Stuendent {
     private $ white_array = [ 'python' ] ;
     private $ name ;
     private $ python = 'I can write Python code' ;
 
     public function __set ( $ name , $ value ) {
         if ( property_exists ( $ this , $ name ) ) {
             $ this -> $ name = $ value ;
         } else {
             throw new \ Exception ( "没有这个私有变量属性" , 1 ) ;
 
         }
 
     }
     public function __get ( $ name ) {
         if ( in_array ( $ name , $ this -> white_array ) ) {
         / / 检测这个属性值 是否在 white _array这个属性里面
         / / 如果在 则返回这个值,如果不在就返回 null
             return $ this -> $ name . "__get" ;
         } else {
             return null ;
         }
 
     }
}
 
$ st = new Stuendent ;
/ / 实例化
$ st -> name = "songhao" ;
echo $ st -> name ; / / 无输出
echo $ st -> python ;
 

输出结果:




  • zeropython 微信公众号 5868037 QQ号 5868037@qq.com QQ邮箱
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值