PHP Reflection API详解


Posted in PHP onMay 12, 2015

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 "来自https://3water.com的测试";
 }
}
 
$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确实能够获取很多有用的信息。

PHP 相关文章推荐
在IIS上安装PHP4.0正式版
Oct 09 PHP
MySql 按时间段查询数据方法(实例说明)
Nov 02 PHP
深入理解PHP原理之Session Gc的一个小概率Notice
Apr 12 PHP
php数组函数序列之array_intersect() 返回两个或多个数组的交集数组
Nov 10 PHP
PHP更新购物车数量(表单部分/PHP处理部分)
May 03 PHP
解析PHP中的正则表达式以及模式匹配
Jun 19 PHP
解析mysql中UNIX_TIMESTAMP()函数与php中time()函数的区别
Jun 24 PHP
php实现aes加密类分享
Feb 16 PHP
php顺序查找和二分查找示例
Mar 27 PHP
PHP中子类重载父类的方法【parent::方法名】
May 06 PHP
用PHP的socket实现客户端到服务端的通信实例详解
Feb 04 PHP
PHP实现简易图形计算器
Aug 28 PHP
php通过curl模拟登陆DZ论坛
May 11 #PHP
PHP中的魔术方法总结和使用实例
May 11 #PHP
php基于curl扩展制作跨平台的restfule 接口
May 11 #PHP
PHP SPL标准库中的常用函数介绍
May 11 #PHP
PHP中的类型约束介绍
May 11 #PHP
PHP SPL标准库之接口(Interface)详解
May 11 #PHP
PHP SPL标准库之文件操作(SplFileInfo和SplFileObject)实例
May 11 #PHP
You might like
PHP 数组基础知识小结
2010/08/20 PHP
php实现购物车功能(下)
2016/01/05 PHP
在Laravel5.6中使用Swoole的协程数据库查询
2018/06/15 PHP
javascript+dom树型菜单类,希望朋友们一起进步
2007/05/03 Javascript
javascript检查日期格式的函数[比较全]
2008/10/17 Javascript
Jquery拖拽并简单保存的实现代码
2010/11/28 Javascript
使用JavaScript检测Firefox浏览器是否启用了Firebug的代码
2010/12/28 Javascript
jquery jqPlot API 中文使用教程(非常强大的图表工具)
2011/08/15 Javascript
Javascript中3个需要注意的运算符
2015/04/02 Javascript
node.js操作mongodb学习小结
2015/04/25 Javascript
详解JavaScript逻辑Not运算符
2015/12/04 Javascript
BootStrap中按钮点击后被禁用按钮的最佳实现方法
2016/09/23 Javascript
javascript使用闭包模拟对象的私有属性和方法
2016/10/05 Javascript
深入了解JavaScript的逻辑运算符(与、或)
2016/12/20 Javascript
在点击div中的p时,如何阻止事件冒泡
2017/02/07 Javascript
React注册倒计时功能的实现
2018/09/06 Javascript
JS实现计算小于非负数n的素数的数量算法示例
2019/02/26 Javascript
JavaScript实现简单计算器
2020/03/19 Javascript
基于vue+echarts数据可视化大屏展示的实现
2020/12/25 Vue.js
Python实现的排列组合计算操作示例
2017/10/13 Python
Python基于多线程操作数据库相关问题分析
2018/07/11 Python
opencv python统计及绘制直方图的方法
2019/01/21 Python
查看python安装路径及pip安装的包列表及路径
2019/04/03 Python
python虚拟环境的安装和配置(virtualenv,virtualenvwrapper)
2019/08/09 Python
CSS3教程(2):网页边框半径和网页圆角
2009/04/02 HTML / CSS
欧洲最大的拼图游戏商店:JigsawPuzzle.co.uk
2018/07/04 全球购物
施华洛世奇新加坡官网:SWAROVSKI新加坡
2020/10/06 全球购物
两年的个人工作自我评价
2014/01/10 职场文书
品德评语大全
2014/05/05 职场文书
机关党员公开承诺书
2014/08/30 职场文书
教师竞聘上岗演讲稿
2014/09/03 职场文书
公务员党员评议表自我鉴定
2014/09/14 职场文书
银行反四风对照检查材料
2014/09/29 职场文书
使用Pytorch训练two-head网络的操作
2021/05/28 Python
Meta增速拉垮,元宇宙难当重任
2022/04/29 数码科技
python数字图像处理之对比度与亮度调整示例
2022/06/28 Python