php reflection是用于获取类、扩展、方法、函数、对象、参数、属性的详细信息。
reflectionclass类获取类相关信息,,如获取属性、方法、文档注释等。
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());
