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 "来自http://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确实能够获取很多有用的信息。

PHP : Reflection API的更多相关文章

  1. Unity5.x在WP8.1中无法使用Reflection API的解决方法

    下班前随便写点,虽然花了不少时间但是最终得到的解决方法还是比较简单的. 第一种方法:使用WinRTLegacy.dll中的类.这个dll在生成的WP project中是自带的无需在unity工程中添加 ...

  2. 【Todo】Java学习笔记 100==100 & Reflection API & Optional类详解 & DIP、IoC、DI & token/cookie/session管理会话方式

    为什么1000 == 1000返回为False,而100 == 100会返回为True?   Link Java Reflection API:Link Java8 Optional 类深度解析: L ...

  3. Why Reflection is slowly?(Trail: The Reflection API)

    反射的使用 反射通常用于在JVM中应用程序运行中需要检查或者修改运行时行为的项目.这是一个相对高级的特性,并且仅仅可以被对深刻理解java原理的开发者使用.这里给出一个警告的意见,反射是一个强大的技术 ...

  4. PHP 反射机制Reflection

    简介 PHP Reflection API是PHP5才有的新功能,它是用来导出或提取出关于类.方法.属性.参数等的详细信息,包括注释. class Reflection { } interface R ...

  5. Java - 反射机制(Reflection)

    Java - 反射机制(Reflection)     > Reflection 是被视为 动态语言的关键,反射机制允许程序在执行期借助于 Reflection API 取得任何类的       ...

  6. Java反射机制剖析(一)-定义和API

    1.     什么是Java反射机制 Java的反射机制是在程序运行时,能够完全知道任何一个类,及其它的属性和方法,并且能够任意调用一个对象的属性和方法.这种运行时的动态获取就是Java的反射机制.其 ...

  7. Java反射(Reflection)

    基本概念 在Java运行时环境中,对于任意一个类,能否知道这个类有哪些属性和方法?对于任意一个对象,能否调用它的任意一个方法? 答案是肯定的. 这种动态获取类的信息以及动态调用对象的方法的功能来自于J ...

  8. 开始使用Reflection

    用于 reflection 的类,如 Method,可以在 java.lang.relfect 包中找到.使用这些类的时候必须要遵循三个步骤:第一步是获得你想操作的类的 java.lang.Class ...

  9. Unity[C#] Reflection Use

      Reflection Reflection是C#程序员的一个最有力工具 最常用的例子来说明反射的用处是一个插件系统.假设你正在创建一个 接受用户创建 的扩展程序,有没有办法预先知道哪些方法这个扩展 ...

随机推荐

  1. Empire C:Basic 4

    一.变量名 1.名字由字母和数字组成,但其第一个字符必须为字母. 2.变量名不要以下划线开头. 3.变量名使用小写字母,符号常量名全部使用大写字母. 二.数据类型及长度 1.char 字符型 占用一个 ...

  2. NTFS 权限讲解 ACL

    节选自:Securing Windows Server 2003 4.1 Protecting Files with NTFS File Permissions The primary techniq ...

  3. phpcmsv9的评论分表策略

    comment_table表 comment表 comment_data_x表 我们留意到: comment_table表统计每个comment_data_x表里面有多少条记录, comment表只是 ...

  4. Linux内核设计第二周——操作系统工作原理

    Linux内核设计第二周 ——操作系统工作原理 作者:宋宸宁(20135315) 一.实验过程 图1 执行效果 从图中可以看出,每执行my_ start_ kernel函数两次或一次,my_ time ...

  5. 64位win10系统无法安装.Net framework3.5的解决方法

    前言 Win10不会自带安装.net framework 3.5,而很多软件必须基于.net framework 3.5 sp1(例如:Sql Server 2014),从官网上下载的.net fra ...

  6. oracle 判断字符串是否日期格式

    select case when to_char(TO_DATE(NVL('2015- 8', 'a'), 'yyyy-mm'),'yyyy-mm')='2015- 8' then 1 else 0 ...

  7. ios之json,xml解析

    JSON解析步骤: 1.获取json文件路径 NSString*path = [[NSBundle mainBundle] pathForResource:@"Teacher"of ...

  8. k8s入门系列之介绍篇

    •Kubernetes介绍1.背景介绍 云计算飞速发展 - IaaS - PaaS - SaaS Docker技术突飞猛进 - 一次构建,到处运行 - 容器的快速轻量 - 完整的生态环境2.什么是ku ...

  9. [curator] Netflix Curator 使用

    curator简介 Netflix curator 是Netflix公司开源的一个Zookeeper client library,用于简化zookeeper客户端编程,包含一下几个模块: curat ...

  10. C# 中的多线程(转载)

    关于多线程的系列,翻译自国外大牛的文章,值得推荐 原文地址:https://blog.gkarch.com/topic/threading.html