您好,欢迎访问一九零五行业门户网

PHP的Reflection反射机制的介绍

这篇文章主要介绍了关于php的reflection反射机制的介绍,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
php5添加了一项新的功能:reflection。这个功能使得程序员可以
reverse-engineer[逆向工程] class, interface,function,method and extension[扩展库支持]。
通过php代码,就可以得到某object的所有信息,并且可以和它交互。
如假设以下person类:
1 class person {  2     /**  3      * for the sake of demonstration, were setting this private  4      */  5     private $_allowdynamicattributes = false;  6       7     /**  8      * type=primary_autoincrement  9      */ 10     protected $id = 0; 11      12     /** 13      * type=varchar length=255 null 14      */ 15     protected $name; 16      17     /** 18      * type=text null19      */ 20     protected $biography; 21     public function getid() { 22         return $this->id; 23     } 24     public function setid($v) { 25         $this->id = $v; 26     } 27     public function getname() { 28         return $this->name; 29     } 30     public function setname($v) { 31         $this->name = $v; 32     } 33     public function getbiography() { 34         return $this->biography; 35     } 36     public function setbiography($v) { 37         $this->biography = $v; 38     } 39 }
通过reflectionclass,我们可以得到person类的以下信息:
常量 contants
属性 property names
方法 method names
静态属性 static properties
命名空间 namespace
person类是否为final或者abstract
只要把类名person传递给reflectionclass就可以了:
1 $class = new reflectionclass('person');
* 获取属性(properties):
1 $properties = $class->getproperties();2 foreach($properties as $property) {3     echo $property->getname().\n;4 }5 // 输出:6 // _allowdynamicattributes7 // id8 // name9 // biography
默认情况下,reflectionclass会获取到所有的属性,private 和 protected的也可以。如果只想获取到private属性,就要额外传个参数:
1 $private_properties = $class->getproperties(reflectionproperty::is_private);
可用参数列表:
reflectionproperty::is_static
reflectionproperty::is_public
reflectionproperty::is_protected
reflectionproperty::is_private
如果要同时获取public 和private 属性,就这样写:reflectionproperty::is_public | reflectionproperty::is_protected
通过$property->getname()可以得到属性名,通过getdoccomment可以得到写给property的注释。
1 foreach($properties as $property) { 2     if($property->isprotected()) {  3         $docblock = $property->getdoccomment();  4         preg_match('/ type\=([a-z_]*) /', $property->getdoccomment(), $matches);  5         echo $matches[1].\n;  6     }  7 }  8 // output:  9 // primary_autoincrement 10 // varchar 11 // text
有点不可思议了吧。竟然连注释都可以取到。
* 获取方法(methods):通过getmethods() 来获取到类的所有methods。返回的是reflectionmethod对象的数组。
不再演示。
* 最后,通过reflectionmethod来调用类里面的method。
$data = array(id => 1, name => chris, biography => i am am a php developer);foreach($data as $key => $value) {    if(!$class->hasproperty($key)) {        throw new exception($key. is not a valid property);    }     if(!$class->hasmethod(get.ucfirst($key))) {            throw new exception($key. is missing a getter);    }     if(!$class->hasmethod(set.ucfirst($key))) {            throw new exception($key. is missing a setter);    }     // make a new object to interact with    $object = new person();     // get the getter method and invoke it with the value in our data array    $setter = $class->getmethod(set.ucfirst($key));        $ok = $setter->invoke($object, $value);     // get the setter method and invoke it    $setter = $class->getmethod(get.ucfirst($key));        $objvalue = $setter->invoke($object);     // now compare    if($value == $objvalue) {            echo getter or setter has modified the data.\n;    } else {            echo getter and setter does not modify the data.\n;   }}
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注!
相关推荐:
php 抓取网页内容与图片的方法
php导入进度条类
php的作用域
以上就是php的reflection反射机制的介绍的详细内容。
其它类似信息

推荐信息