欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP Reflection API详解

程序员文章站 2023-01-24 23:50:28
php reflection api是php5才有的新功能,它是用来导出或提取出关于类、方法、属性、参数等的详细信息,包括注释。 php reflection api有:...

php reflection api是php5才有的新功能,它是用来导出或提取出关于类、方法、属性、参数等的详细信息,包括注释。

php reflection api有:

class reflection { }
interface reflector { }
class reflectionexception extends exception { }
class reflectionfunction implements reflector { }
class reflectionparameter implements reflector { }
class reflectionmethod extends reflectionfunction { }
class reflectionclass implements reflector { }
class reflectionobject extends reflectionclass { }
class reflectionproperty implements reflector { }
class reflectionextension implements reflector { } 

具体api说明:

①reflection类

<?php
class reflection
{
  public static mixed export(reflector r [,bool return])
  //导出一个类或方法的详细信息
  public static array getmodifiernames(int modifiers)
  //取得修饰符的名字
}
?>

②reflectionexception类

该类继承标准类,没特殊方法和属性。

③reflectionfunction类

<?php
class reflectionfunction implements reflector
{
  final private __clone()
  public object __construct(string name)
  public string __tostring()
  public static string export()
  //导出该函数的详细信息
  public string getname()
  //取得函数名
  public bool isinternal()
  //测试是否为系统内部函数
  public bool isuserdefined()
  //测试是否为用户自定义函数
  public string getfilename()
  //取得文件名,包括路径名
  public int getstartline()
  //取得定义函数的起始行
  public int getendline()
  //取得定义函数的结束行
  public string getdoccomment()
  //取得函数的注释
  public array getstaticvariables()
  //取得静态变量
  public mixed invoke(mixed* args)
  //调用该函数,通过参数列表传参数
  public mixed invokeargs(array args)
  //调用该函数,通过数组传参数
  public bool returnsreference()
  //测试该函数是否返回引用
  public reflectionparameter[] getparameters()
  //取得该方法所需的参数,返回值为对象数组
  public int getnumberofparameters()
  //取得该方法所需的参数个数
  public int getnumberofrequiredparameters()
  //取得该方法所需的参数个数
}
?>

④reflectionparameter类:

<?php
class reflectionparameter implements reflector
{
  final private __clone()
  public object __construct(string name)
  public string __tostring()
  public static string export()
  //导出该参数的详细信息
  public string getname()
  //取得参数名
  public bool ispassedbyreference()
  //测试该参数是否通过引用传递参数
  public reflectionclass getclass()
  //若该参数为对象,返回该对象的类名
  public bool isarray()
  //测试该参数是否为数组类型
  public bool allowsnull()
  //测试该参数是否允许为空
  public bool isoptional()
  //测试该参数是否为可选的,当有默认参数时可选
  public bool isdefaultvalueavailable()
  //测试该参数是否为默认参数
  public mixed getdefaultvalue()
  //取得该参数的默认值
}
?>

⑤reflectionclass类:

<?php
class reflectionclass implements reflector
{
  final private __clone()
  public object __construct(string name)
  public string __tostring()
  public static string export()
  //导出该类的详细信息
  public string getname()
  //取得类名或接口名
  public bool isinternal()
  //测试该类是否为系统内部类
  public bool isuserdefined()
  //测试该类是否为用户自定义类
  public bool isinstantiable()
  //测试该类是否被实例化过
  public bool hasconstant(string name)
  //测试该类是否有特定的常量
  public bool hasmethod(string name)
  //测试该类是否有特定的方法
  public bool hasproperty(string name)
  //测试该类是否有特定的属性
  public string getfilename()
  //取得定义该类的文件名,包括路径名
  public int getstartline()
  //取得定义该类的开始行
  public int getendline()
  //取得定义该类的结束行
  public string getdoccomment()
  //取得该类的注释
  public reflectionmethod getconstructor()
  //取得该类的构造函数信息
  public reflectionmethod getmethod(string name)
  //取得该类的某个特定的方法信息
  public reflectionmethod[] getmethods()
  //取得该类的所有的方法信息
  public reflectionproperty getproperty(string name)
  //取得某个特定的属性信息
  public reflectionproperty[] getproperties()
  //取得该类的所有属性信息
  public array getconstants()
  //取得该类所有常量信息
  public mixed getconstant(string name)
  //取得该类特定常量信息
  public reflectionclass[] getinterfaces()
  //取得接口类信息
  public bool isinterface()
  //测试该类是否为接口
  public bool isabstract()
  //测试该类是否为抽象类
  public bool isfinal()
  //测试该类是否声明为final
  public int getmodifiers()
  //取得该类的修饰符,返回值类型可能是个资源类型
  //通过reflection::getmodifiernames($class->getmodifiers())进一步读取
  public bool isinstance(stdclass object)
  //测试传入的对象是否为该类的一个实例
  public stdclass newinstance(mixed* args)
  //创建该类实例
  public reflectionclass getparentclass()
  //取得父类
  public bool issubclassof(reflectionclass class)
  //测试传入的类是否为该类的父类
  public array getstaticproperties()
  //取得该类的所有静态属性
  public mixed getstaticpropertyvalue(string name [, mixed default])
  //取得该类的静态属性值,若private,则不可访问
  public void setstaticpropertyvalue(string name, mixed value)
  //设置该类的静态属性值,若private,则不可访问,有悖封装原则
  public array getdefaultproperties()
  //取得该类的属性信息,不含静态属性
  public bool isiterateable()
  public bool implementsinterface(string name)
  //测试是否实现了某个特定接口
  public reflectionextension getextension()
  public string getextensionname()
}
?>

⑥reflectionmethod类:

<?php
class reflectionmethod extends reflectionfunction
{
  public __construct(mixed class, string name)
  public string __tostring()
  public static string export()
  //导出该方法的信息
  public mixed invoke(stdclass object, mixed* args)
  //调用该方法
  public mixed invokeargs(stdclass object, array args)
  //调用该方法,传多参数
  public bool isfinal()
  //测试该方法是否为final
  public bool isabstract()
  //测试该方法是否为abstract
  public bool ispublic()
  //测试该方法是否为public
  public bool isprivate()
  //测试该方法是否为private
  public bool isprotected()
  //测试该方法是否为protected
  public bool isstatic()
  //测试该方法是否为static
  public bool isconstructor()
  //测试该方法是否为构造函数
  public bool isdestructor()
  //测试该方法是否为析构函数
  public int getmodifiers()
  //取得该方法的修饰符
  public reflectionclass getdeclaringclass()
  //取得该方法所属的类
  // inherited from reflectionfunction
  final private __clone()
  public string getname()
  public bool isinternal()
  public bool isuserdefined()
  public string getfilename()
  public int getstartline()
  public int getendline()
  public string getdoccomment()
  public array getstaticvariables()
  public bool returnsreference()
  public reflectionparameter[] getparameters()
  public int getnumberofparameters()
  public int getnumberofrequiredparameters()
}
?>

⑦reflectionproperty类:

<?php
class reflectionproperty implements reflector
{
  final private __clone()
  public __construct(mixed class, string name)
  public string __tostring()
  public static string export()
  //导出该属性的详细信息
  public string getname()
  //取得该属性名
  public bool ispublic()
  //测试该属性名是否为public
  public bool isprivate()
  //测试该属性名是否为private
  public bool isprotected()
  //测试该属性名是否为protected
  public bool isstatic()
  //测试该属性名是否为static
  public bool isdefault()
  public int getmodifiers()
  //取得修饰符
  public mixed getvalue(stdclass object)
  //取得该属性值
  public void setvalue(stdclass object, mixed value)
  //设置该属性值
  public reflectionclass getdeclaringclass()
  //取得定义该属性的类
  public string getdoccomment()
  //取得该属性的注释
}
?>

⑧reflectionextension类

<?php
class reflectionextension implements reflector {
  final private __clone()
  public __construct(string name)
  public string __tostring()
  public static string export()
  //导出该扩展的所有信息
  public string getname()
  //取得该扩展的名字
  public string getversion()
  //取得该扩展的版本
  public reflectionfunction[] getfunctions()
  //取得该扩展的所有函数
  public array getconstants()
  //取得该扩展的所有常量
  public array getinientries()
  //取得与该扩展相关的,在php.ini中的指令信息
  public reflectionclass[] getclasses()
  public array getclassnames()
}

?> 

使用例子:

<?php
class person{
 private $_name;
 
 public $age;
 
 public function __construct(){
 $this->sex = "male";
 }
 
 public function action(){
 echo "来自//www.jb51.net的测试";
 }
}
 
$class = new reflectionclass('person');
//获取属性
foreach($class->getproperties() as $property) {
  echo $property->getname()."\n";
}
//获取方法
print_r($class->getmethods());
 
$p1 = new person();
$obj = new reflectionobject($p1);
 
//获取对象和类的属性
print_r($obj->getproperties());

很明显上面代码中对象和类获取的属性是不同的,这是因为对象进行了contruct实例化,因此多了sex属性,php reflection确实能够获取很多有用的信息。