这篇文章主要介绍了php 反射(reflection)使用实例,本文讲解了reflectionclass、reflectionextension、 reflectionfunction、reflectionmethod、reflectionobject、reflectionparameter等类的使用实例,需要的朋友可以参考下
php reflection是用于获取类、扩展、方法、函数、对象、参数、属性的详细信息。
reflectionclass类获取类相关信息,如获取属性、方法、文档注释等。
<?php class person { /** * for the sake of demonstration, we"re setting this private */ private $_allowdynamicattributes = false; /** type=primary_autoincrement */ protected $id = 0; /** type=varchar length=255 null */ protected $name; /** type=text null */ protected $biography; public function getid() { return $this->id; } public function setid($v) { $this->id = $v; } public function getname() { return $this->name; } public function setname($v) { $this->name = $v; } public function getbiography() { return $this->biography; } public function setbiography($v) { $this->biography = $v; }} //导出类reflectionclass::export('person'); $r = new reflectionclass('person'); //获取所有属性print_r($r->getproperties()); /** * 获取指定属性 * reflectionproperty::is_static * reflectionproperty::is_public * reflectionproperty::is_protected * reflectionproperty::is_private */print_r($r->getproperties(reflectionproperty::is_private)); //获取注释print_r($r->getproperty('id')->getdoccomment()); //获取方法print_r($r->getmethods());
reflectionextension 类用于获取扩展相关信息
$re = new reflectionextension('reflection');print_r($re->getclasses()); //扩展的所有类print_r($re->getclassnames()); //扩展所有类名 $dom = new reflectionextension('mysql');print_r($dom->getconstants());//扩展常量print_r($dom->getdependencies());//该扩展依赖print_r($dom->getfunctions());//扩展方法print_r($dom->getinientries());//扩展ini信息print_r($dom->getname());//扩展名称print_r($dom->getversion());//扩展版本print_r($dom->info());//扩展信息print_r($dom->ispersistent());//是否是持久扩展print_r($dom->istemporary()); //是否是临时扩展
reflectionfunction类 用户获取函数相关信息
$rf = new reflectionfunction('array_merge'); foreach($rf->getparameters() as $item) { echo $item . php_eol;}
reflectionmethod类用户获取方法相关信息
class person { public $name; /** * get name of person */ public function getname() { return $this->name; } public function setname($v) { $this->name = $v; }} $rm = new reflectionmethod('person', 'getname'); print_r($rm->ispublic());print_r($rm->getdoccomment());
reflectionobject 类 用于获取对象相关信息
class person { public $name; public function __construct($name) { $this->name = $name; } public function getname() { return $this->name; } public function setname($v) { $this->name = $v; }} $a = new person('a'); $ro = new reflectionobject($a); print_r($ro->getmethods());
reflectionparameter 获取函数或方法参数的相关信息。
class person { public $name; public function __construct($name) { $this->name = $name; } public function getname() { return $this->name; } public function setname($v) { $this->name = $v; }} $p = new reflectionparameter(array('person', 'setname'), 0); print_r($p->getposition()); //0print_r($p->getname()); //v
reflectionproperty 获取类的属性的相关信息。
class person { /** 测试 */ public $name; public function __construct($name) { $this->name = $name; } public function getname() { return $this->name; } public function setname($v) { $this->name = $v; }} $p = new reflectionproperty('person', 'name'); print_r($p->getdoccomment());
总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。
相关推荐:
php读取、正则匹配邮件内容的方法
php实现持久层的方法
php实现简单的get、post、cookie、session等功能
以上就是简述php 反射五种类的使用的详细内容。