PHP头条
热点:

PHP反射API的功能和用途详解


认识php反射API

它是指在PHP运行状态中,扩展分析PHP程序,导出或提取出关于类、方法、属性、参数等的详细信息,包括注释。这种动态获取的信息以及动态调用对象的方法的功能称为反射API。反射是操纵面向对象泛型中元模型的API,其功能十分强大,可帮助我们构建复杂,可扩展的应用。

其用途如:自动加载插件,自动生成文档,甚至可用来扩充PHP语言。

php反射api由若干类组成,可帮助我们用来访问程序的元数据或者同相关的注释交互。借助反射我们可以获取诸如类实现了哪些方法,创建一个类的实例(不同于用new创建),调用一个方法(也不同于常规调用),传递参数,动态调用类的静态方法。

反射api是php内建的oop技术扩展,包括一些类,异常和接口,综合使用他们可用来帮助我们分析其它类,接口,方法,属性,方法和扩展。这些oop扩展被称为反射。

e3d774809dcbe3501b02f07e6afa5935.png

php反射API类

类描 述Reflection为类的摘要信息提供静态函数export()ReflectionClass类信息和工具ReflectionMethod类方法信息和工具ReflectionParameter方法参数信息ReflectionProperty类属性信息ReflectionFunction函数信息和工具ReflectionExtensionPHP扩展信息ReflectionException错误类

使用反射API这些类,我们可以获得在运行时访问对象、函数和脚本中的扩展的信息。通过这些信息我们可以用来分析类或者构建框架。

获取类的信息

我们在工作中使用过一些用于检查类属性的函数,例如:get_class_methods、getProduct等。这些方法对获取详细类信息有很大的局限性。

我们可以通过反射API类:Reflection 和 ReflectionClass 提供的静态方法 export 来获取类的相关信息, export 可以提供类的几乎所有的信息,包括属性和方法的访问控制状态、每个方法需要的参数以及每个方法在脚本文档中的位置。这两个工具类, export 静态方法输出结果是一致的,只是使用方式不同。

ReflectionClass类提供了非常多的工具方法,官方手册给的列表如下:

ReflectionClass::__construct — 初始化 ReflectionClass 类

ReflectionClass::export — 导出一个类

ReflectionClass::getConstant — 获取定义过的一个常量

ReflectionClass::getConstants — 获取一组常量

ReflectionClass::getConstructor — 获取类的构造函数

ReflectionClass::getDefaultProperties — 获取默认属性

ReflectionClass::getDocComment — 获取文档注释

ReflectionClass::getEndLine — 获取最后一行的行数

ReflectionClass::getExtension — 根据已定义的类获取所在扩展的 ReflectionExtension 对象

ReflectionClass::getExtensionName — 获取定义的类所在的扩展的名称

ReflectionClass::getFileName — 获取定义类的文件名

ReflectionClass::getInterfaceNames — 获取接口(interface)名称

ReflectionClass::getInterfaces — 获取接口

ReflectionClass::getMethod — 获取一个类方法的 ReflectionMethod。

ReflectionClass::getMethods — 获取方法的数组

ReflectionClass::getModifiers — 获取类的修饰符

ReflectionClass::getName — 获取类名

ReflectionClass::getNamespaceName — 获取命名空间的名称

ReflectionClass::getParentClass — 获取父类

ReflectionClass::getProperties — 获取一组属性

ReflectionClass::getProperty — 获取类的一个属性的 ReflectionProperty

ReflectionClass::getReflectionConstant — Gets a ReflectionClassConstant for a class's constant

ReflectionClass::getReflectionConstants — Gets class constants

ReflectionClass::getShortName — 获取短名

ReflectionClass::getStartLine — 获取起始行号

ReflectionClass::getStaticProperties — 获取静态(static)属性

ReflectionClass::getStaticPropertyValue — 获取静态(static)属性的值

ReflectionClass::getTraitAliases — 返回 trait 别名的一个数组

ReflectionClass::getTraitNames — 返回这个类所使用 traits 的名称的数组

ReflectionClass::getTraits — 返回这个类所使用的 traits 数组

ReflectionClass::hasConstant — 检查常量是否已经定义

ReflectionClass::hasMethod — 检查方法是否已定义

ReflectionClass::hasProperty — 检查属性是否已定义

ReflectionClass::implementsInterface — 接口的实现

ReflectionClass::inNamespace — 检查是否位于命名空间中

ReflectionClass::isAbstract — 检查类是否是抽象类(abstract)

ReflectionClass::isAnonymous — 检查类是否是匿名类

ReflectionClass::isCloneable — 返回了一个类是否可复制

ReflectionClass::isFinal — 检查类是否声明为 final

ReflectionClass::isInstance — 检查类的实例

ReflectionClass::isInstantiable — 检查类是否可实例化

ReflectionClass::isInterface — 检查类是否是一个接口(interface)

ReflectionClass::isInternal — 检查类是否由扩展或核心在内部定义

ReflectionClass::isIterateable — 检查是否可迭代(iterateable)

ReflectionClass::isSubclassOf — 检查是否为一个子类

ReflectionClass::isTrait — 返回了是否为一个 trait

ReflectionClass::isUserDefined — 检查是否由用户定义的

ReflectionClass::newInstance — 从指定的参数创建一个新的类实例

ReflectionClass::newInstanceArgs — 从给出的参数创建一个新的类实例。

ReflectionClass::newInstanceWithoutConstructor — 创建一个新的类实例而不调用它的构造函数

ReflectionClass::setStaticPropertyValue — 设置静态属性的值

ReflectionClass::__toString — 返回 ReflectionClass 对象字符串的表示形式。

除了获取类的相关信息,还可以获取 ReflectionClass 对象提供自定义类所在的文件名及文件中类的起始和终止行等相关源代码信息。

function getClassSource(ReflectionClass $class) {

$path = $class->getFileName(); // 获取类文件的绝对路径

$lines = @file($path); // 获得由文件中所有行组成的数组

$from = $class->getStartLine(); // 提供类的起始行

$to = $class->getEndLine(); // 提供类的终止行

$len = $to - $from + 1;

return implode(array_slice($lines, $from - 1, $len));

}

检查方法

类似于检查类,ReflectionMethod 对象可以用于检查类中的方法。

获得 ReflectionMethod 对象的方法有两种:

第一种是通过 ReflectionClass::getMethods() 获得 ReflectionMethod 对象的数组,这种方式的好处是不用提前知道方法名,会返回类中所有方法的 ReflectionMethod 对象。

第二种是直接使用 ReflectionMethod 类实例化对象,这种方式只能获取一个类方法对象,需要提前知道方法名。

ReflectionMethod 对象的工具方法:

ReflectionMethod::__construct — ReflectionMethod 的构造函数

ReflectionMethod::export — 输出一个回调方法

ReflectionMethod::getClosure — 返回一个动态建立的方法调用接口,译者注:可以使用这个返回值直接调用非公开方法。

ReflectionMethod::getDeclaringClass — 获取反射函数调用参数的类表达

ReflectionMethod::getModifiers — 获取方法的修饰符

ReflectionMethod::getPrototype — 返回方法原型 (如果存在)

ReflectionMethod::invoke — Invoke

ReflectionMethod::invokeArgs — 带参数执行

ReflectionMethod::isAbstract — 判断方法是否是抽象方法

ReflectionMethod::isConstructor — 判断方法是否是构造方法

ReflectionMethod::isDestructor — 判断方法是否是析构方法

ReflectionMethod::isFinal — 判断方法是否定义 final

ReflectionMethod::isPrivate — 判断方法是否是私有方法

ReflectionMethod::isProtected — 判断方法是否是保护方法 (protected)

ReflectionMethod::isPublic — 判断方法是否是公开方法

ReflectionMethod::isStatic — 判断方法是否是静态方法

ReflectionMethod::setAccessible — 设置方法是否访问

ReflectionMethod::__toString — 返回反射方法对象的字符串表达

在PHP5中,如果被检查的方法只返回对象(即使对象是通过引用赋值或传递的),那么 ReflectionMethod::retursReference() 不会返回 true。只有当被检测的方法已经被明确声明返回引用(在方法名前面有&符号)时,ReflectionMethod::returnsReference() 才返回 true。

检查方法参数

类似于检查方法,ReflectionParameter 对象可以用于检查类中的方法,该对象可以告诉你参数的名称,变量是否可以按引用传递,还可以告诉你参数类型提示和方法是否接受空值作为参数。

获得 ReflectionParameter 对象的方法有同样两种,这和获取 ReflectionMethod 对象非常类似:

第一种是通过 ReflectionMethod::getParameters() 方法返回 ReflectionParameter 对象数组,这种方法可以获取到一个方法的全部参数对象。

第二种是直接使用 ReflectionParameter 类实例化获取对象,这种方法只能获取到单一参数的对象。

ReflectionParameter 对象的工具方法:

ReflectionParameter::allowsNull — Checks if null is allowed

ReflectionParameter::canBePassedByValue — Returns whether this parameter can be passed by value

ReflectionParameter::__clone — Clone

ReflectionParameter::__construct — Construct

ReflectionParameter::export — Exports

ReflectionParameter::getClass — Get the type hinted class

ReflectionParameter::getDeclaringClass — Gets declaring class

ReflectionParameter::getDeclaringFunction — Gets declaring function

ReflectionParameter::getDefaultValue — Gets default parameter value

ReflectionParameter::getDefaultValueConstantName — Returns the default value's constant name if default value is constant or null

ReflectionParameter::getName — Gets parameter name

ReflectionParameter::getPosition — Gets parameter position

ReflectionParameter::getType — Gets a parameter's type

ReflectionParameter::hasType — Checks if parameter has a type

ReflectionParameter::isArray — Checks if parameter expects an array

ReflectionParameter::isCallable — Returns whether parameter MUST be callable

ReflectionParameter::isDefaultValueAvailable — Checks if a default value is available

ReflectionParameter::isDefaultValueConstant — Returns whether the default value of this parameter is constant

ReflectionParameter::isOptional — Checks if optional

ReflectionParameter::isPassedByReference — Checks if passed by reference

ReflectionParameter::isVariadic — Checks if the parameter is variadic

ReflectionParameter::__toString — To string

ReflectionMethod::getParameters

$method = new ReflectionMethod('Student', 'setName');

$params = $method->getParameters();

var_dump($params);

结语

php的反射API功能非常的强大,它可以将一个类的详细信息获取出来。我们可以通过反射API编写个类来动态调用Module对象,该类可以自由加载第三方插件并集成进已有的系统。而不需要把第三方的代码硬编码进原有的代码中。虽然实际开发中使用反射情况比较少,但了解反射API对工作中对代码结构的了解和开发业务模式帮助还是非常大的。

PHP中的反射API就像Java中的java.lang.reflect包一样。它由一系列可以分析属性、方法和类的内置类组成。它在某些方面和对象函数相似,比如get_class_vars(),但是更加灵活,而且可以提供更多信息。反射API也可与PHP最新的面向对象特性一起工作,如访问控制、接口和抽象类。旧的类函数则不太容易与这些新特性一起使用。看过框架源码的朋友应该对PHP的反射机制有一定的了解,像是依赖注入,对象池,类加载,一些设计模式等等,都用到了反射机制。



www.phpzy.comtrue/phpzx/53474.htmlTechArticlePHP反射API的功能和用途详解 认识php反射API 它是指在PHP运行状态中扩展分析PHP程序导出或提取出关于类、方法、属性、参数等的详细信息包括注释。这种动态获取的信息以及动态调用对...

相关文章

PHP之友评论

今天推荐