php拦截器 __get( )使用方法

访问php类中未定义的属性时会调用__get()方法,给未定义的属性赋值调用__set()方法

以__get()为例,说明php拦截器的用法

 

 1 <?php
 2 
 3 class A{
 4     function __get($value){
 5         $method = "get{$value}";
 6         if(method_exists($this,$method)){
 7             return $this->$method();
 8         }
 9         
10     }
11     function getName(){
12         return "jxu";
13     }
14 }
15 
16 $a = new A();
17 print $a->name;

 

posted @ 2015-12-03 13:57  junxu  阅读(303)  评论(0编辑  收藏  举报