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 相关文章推荐
资料注册后发信小技巧
Oct 09 PHP
php提交表单时判断 if($_POST[submit])与 if(isset($_POST[submit])) 的区别
Feb 08 PHP
destoon安全设置中需要设置可写权限的目录及文件
Jun 21 PHP
十大使用PHP框架的理由
Sep 26 PHP
php根据数据id自动生成编号的实现方法
Oct 16 PHP
PHP接口并发测试的方法(推荐)
Dec 15 PHP
php 访问oracle 存储过程实例详解
Jan 08 PHP
Yii2 批量插入、更新数据实例
Mar 15 PHP
详谈php中 strtr 和 str_replace 的效率问题
May 14 PHP
Laravel 中创建 Zip 压缩文件并提供下载的实现方法
Apr 02 PHP
ThinkPHP5.1+Ajax实现的无刷新分页功能示例
Feb 10 PHP
php计数排序算法的实现代码(附四个实例代码)
Mar 31 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 pthreads多线程的安装与使用
2016/01/19 PHP
浅谈PHP中的
2016/04/23 PHP
Laravel框架路由管理简单示例
2019/05/07 PHP
PHP中关于php.ini参数优化详解
2020/02/28 PHP
代码生成器 document.write()
2007/04/15 Javascript
jQuery 选择器理解
2010/03/16 Javascript
通过jquery还原含有rowspan、colspan的table的实现方法
2012/02/10 Javascript
Firefox/Chrome/Safari的中可直接使用$/$$函数进行调试
2012/02/13 Javascript
深入理解JavaScript系列(31):设计模式之代理模式详解
2015/03/03 Javascript
javascript实现动态导入js与css等静态资源文件的方法
2015/07/25 Javascript
多种JQuery循环滚动文字图片效果代码
2020/06/23 Javascript
vue-drag-chart 拖动/缩放图表组件的实例代码
2020/04/10 Javascript
jQuery 隐藏/显示效果函数用法实例分析
2020/05/20 jQuery
跟老齐学Python之私有函数和专有方法
2014/10/24 Python
Python实现进程同步和通信的方法
2018/01/02 Python
python实现三维拟合的方法
2018/12/29 Python
python修改字典键(key)的方法
2019/08/05 Python
python sorted方法和列表使用解析
2019/11/18 Python
Python求凸包及多边形面积教程
2020/04/12 Python
Python flask框架如何显示图像到web页面
2020/06/03 Python
CSS3下的渐变文字效果实现示例
2018/03/02 HTML / CSS
HTML5本地存储之Web Storage详解
2016/07/04 HTML / CSS
玛蒂尔达简服装:Matilda Jane Clothing
2019/02/13 全球购物
"引用"与指针的区别是什么
2016/09/07 面试题
linux比较文件内容的命令是什么
2015/09/23 面试题
机电专业毕业生求职信
2013/10/27 职场文书
路政管理专业个人自荐信范文
2013/11/30 职场文书
卫生厅领导班子党的群众路线教育实践活动整改措施
2014/09/20 职场文书
2015年医院药剂科工作总结
2015/05/04 职场文书
入党积极分子党小组意见
2015/06/02 职场文书
运动会通讯稿600字
2015/07/20 职场文书
2016小学教师读书心得体会
2016/01/13 职场文书
2019年关于小学生课外阅读情况的分析报告
2019/12/02 职场文书
OpenCV-Python实现油画效果的实例
2021/06/08 Python
python程序的组织结构详解
2021/12/06 Python
Spring Data JPA框架Repository自定义实现
2022/04/28 Java/Android