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()); //0
print_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());
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!