反射

2021-04-06

添加了对类、接口、函数、方法和扩展进行反向工程的能力。 此外,反射 API 提供了方法来取出函数、类和方法中的文档注释。

Reflection类

反射(reflection)类

//导出一个反射,如果参数 return 设为 true,导出结果将作为 string 返回,否则返回 null
Reflection::export(Reflector $reflector , bool $return = false):string
  reflector 导出的反射。
  return 设为 true 时返回导出结果,设为 false(默认值)则忽略返回

//获取修饰符的名称
Reflection::getModifierNames(int $modifiers):array
  modifiers 根据标志位域获取修饰符

ReflectionClass类

报告了一个类的有关信息

常量

//指示了类是一个抽象类(abstract), 因为它有抽象(abstract)方法
ReflectionClass::IS_IMPLICIT_ABSTRACT

//指示了类是一个抽象类(abstract), 因为它已明确定义
ReflectionClass::IS_EXPLICIT_ABSTRACT

//指示这是一个 final 类
ReflectionClass::IS_FINAL

方法

初始化类

//初始化 ReflectionClass 类
ReflectionClass::__construct(mixed $argument)
  argument 既可以是包含类名的字符串(string)也可以是对象(object)

获取类

//导出一个反射后的类,果参数 return 设为 true,导出结果将作为 string 返回,否则返回 null
ReflectionClass::export(mixed $argument , bool $return = false):string
  argument 导出的反射。
  return 设为 true 时返回导出结果,设为 false(默认值)则忽略返回

//获取类的名称
ReflectionClass::getName():string

//获取类的短名,就是不含命名空间(namespace)的那一部分。
ReflectionClass::getShortName():string

//获取命名空间(namespace)的名称
ReflectionClass::getNamespaceName():string

//获取定义的类所在的扩展的名称。如果是用户定义的类,则返回 false
ReflectionClass::getExtensionName():string

设置类静态属性

//设置静态属性的值
ReflectionClass::setStaticPropertyValue(string $name , string $value):void
  name 属性的名称。
  value 属性的值

获取类属性

//获取类的默认属性(包括了继承的属性)默认属性的数组,其键是属性的名称,其值是属性的默认值或者在属性没有默认值时是 null。 这个函数不区分静态和非静态属性,也不考虑可见性修饰符。
ReflectionClass::getDefaultProperties():array

//获取反射过的属性
ReflectionClass::getProperties(int $filter = ?):array
  filter 可选的过滤器,过滤为所需类型的属性。它使用 ReflectionProperty 常量 来配置,默认获取所有类型的属性

//获取类的一个属性
ReflectionClass::getProperty(string $name):ReflectionProperty
  name 属性名

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

//获取这个类里静态(static)属性的值
ReflectionClass::getStaticPropertyValue(string $name , mixed &$def_value = ?):mixed
  name 静态属性的名称,来返回它的值。
  def_value 假如类没有定义 name 的 static 属性,将返回一个默认值。 如果属性不存在,并且省略了此参数,将会抛出 ReflectionException 。

获取类常量

//获取定义过的一个常量。
ReflectionClass::getConstant(string $name):mixed
  name 常量的名称

//获取某个类的全部已定义的常量,不管可见性如何定义。常量名是数组的键,常量的值是数组的值
ReflectionClass::getConstants():mixed

//获取类的一个常量
ReflectionClass::getReflectionConstant(string $name):ReflectionClassConstant|false
  name 类的常量名称

//获取类常量
ReflectionClass::getReflectionConstants(int|null $filter = null):array
  filter 可选的过滤器,用于过滤所需的恒定可见性。它是使用ReflectionClassConstant常量配置的,并且默认为所有常量可见性

获取类方法

//获取已反射的类的构造函数,返回反射了类的构造函数,或者当类不存在构造函数时返回 null
ReflectionClass::getConstructor():ReflectionMethod

//获取一个类方法的,如果方法不存在则会抛出 ReflectionException 异常
ReflectionClass::getMethod(string $name):ReflectionMethod
  name 要反射的方法名称

//获取类的方法的一个数组,返回包含每个方法 ReflectionMethod 对象的数组
ReflectionClass::getMethods(int $filter = ?):array
  filter 过滤结果为仅包含某些属性的方法。默认不过滤,ReflectionMethod类常量的按位或(OR)

获取注释

//如果存在则返回文档注释,否则返回 false
ReflectionClass::getDocComment():string

获取父类

//获取父类
ReflectionClass::getParentClass():ReflectionClass

获取接口

//获取接口(interface)名称,接口(interface)的名称是数组的值
ReflectionClass::getInterfaceNames():array

//获取接口,数组键是接口(interface)的名称,数组的值是 ReflectionClass 对象
ReflectionClass::getInterfaces():array

获取trait

//返回 trait 别名的一个数组,返回了一个数组,新的方法名位于键中,原始名称(格式是 "TraitName::original")位于数组的值中.出现一个错误的情况下返回 null
ReflectionClass::getTraitAliases():array

//返回这个类所使用 traits 的名称的数组,返回的数组的值包含了 trait 的名称。 出现错误的情况下返回 null
ReflectionClass::getTraitNames():array

//返回这个类所使用的 traits 数组,返回了一个数组,键是 trait 的名称,值是 trait 实例的 ReflectionClass。 出现错误的情况下返回 null
ReflectionClass::getTraits():array

获取类文件

//获取类被定义的文件的文件名。如果这个类是在 PHP 核心或 PHP 扩展中定义的,则返回 false
ReflectionClass::getFileName():string

//获取起始行号
ReflectionClass::getStartLine():int

//返回用户定义的类最后一行的行数,如果未知则返回 false
ReflectionClass::getEndLine():int

获取类修饰符

//返回这个类访问修饰符的位字段
ReflectionClass::getModifiers():int

检查定义

//检查类中是否已经定义了指定的常量
ReflectionClass::hasConstant(string $name):bool
  name 要被检查的常量名称

//检查指定的属性是否已定义
ReflectionClass::hasProperty(string $name):bool
  name 待检查的属性的名称

//检查一个类中指定的方法是否已定义
ReflectionClass::hasMethod(string $name):bool
  name 要检查的方法的名称

//检查这个类是否定义于一个命名空间中里
ReflectionClass::inNamespace():bool

检查类

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

//检查它是否实现了一个接口(interface)
ReflectionClass::implementsInterface(string $interface):bool
  interface 接口(interface)的名称

//检查是否为一个trait
ReflectionClass::isTrait():bool

//检查这个类是否是抽象类(abstract)
ReflectionClass::isAbstract():bool

//检查一个类是否为指定类的子类,或者实现了指定的接口
ReflectionClass::isSubclassOf(string $class):bool
  class 被检查的类名

//检查一个类是否由用户定义,和内置相对
ReflectionClass::isUserDefined():bool

//检查类是否是匿名类
ReflectionClass::isAnonymous():bool

//检查类是否声明为 final
ReflectionClass::isFinal():bool

//检查这个类是否可复制
ReflectionClass::isCloneable():bool

//检查类是否由扩展或核心在内部定义,与用户定义相反
ReflectionClass::isInternal():bool

//检查此类是否可迭代(即可以在foreach内部使用)
ReflectionClass::isIterable():bool

//检查一个类是否可迭代(iterateable)
ReflectionClass::isIterateable():bool

//检查对象是否为一个类的实例
ReflectionClass::isInstance(object $object):bool
  object 待比较的对象

//检查这个类是否可实例化
ReflectionClass::isInstantiable():bool

创建实例

//创建类的新的实例。给出的参数将会传递到类的构造函数,如果类的构造函数不是 public 的将会导致一个 ReflectionException。当 args 指定了一个或多个参数,而类不具有构造函数时,将导致一个 ReflectionException
ReflectionClass::newInstance(mixed $args , mixed $... = ?):object
  args 接受可变数目的参数,用于传递到类的构造函数,和 call_user_func() 很相似

//创建一个类的新实例,给出的参数将传递到类的构造函数
ReflectionClass::newInstanceArgs(array $args = ?):object
  args 这个参数以 array 形式传递到类的构造函数

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

其他

//返回 ReflectionClass 对象字符串的表示形式
ReflectionClass::__toString():string

//获取已定义类的扩展的 ReflectionExtension 对象,如果是用户定义的类则返回 null。
ReflectionClass::getExtension():ReflectionExtension

ReflectionObject类

报告了一个对象(object)的相关信息,继承了ReflectionClass

//构造一个ReflectionObject。
ReflectionObject::__construct(object $object)
  object 对象实例。

//导出反射
ReflectionObject::export(string $argument , bool $return = ?):string
  argument 导出的反射。
  return 设为 true 时返回导出结果,设为 false(默认值)则忽略返回

ReflectionClassConstant类

报告有关类常量信息

常量

//指示公共
ReflectionClassConstant::IS_PUBLIC

//指示受保护
ReflectionClassConstant::IS_PROTECTED

//指示私有
ReflectionClassConstant::IS_PRIVATE

方法

创建对象

//构造一个新的ReflectionClassConstant对象
ReflectionClassConstant::__construct(object|string $class , string $constant)
  class 包含要反映的类名称的字符串,或对象
  constant 类常量的名称

获取类

//获取声明的类
ReflectionClassConstant::getDeclaringClass():ReflectionClass

获取注释

//从类常量获取文档注释。
ReflectionClassConstant::getDocComment():string|false

获取修饰符

//获取类常量修饰符
ReflectionClassConstant::getModifiers():int

获取常量

//获取常量的名称
ReflectionClassConstant::getName():string|false

//获取类常量的值
ReflectionClassConstant::getValue():mixed

检查修饰符

//检查类常量是否为私有
ReflectionClassConstant::isPrivate():bool

//检查类常量是否受保护
ReflectionClassConstant::isProtected():bool

//检查类常量是否为public
ReflectionClassConstant::isPublic():bool

其他

//返回ReflectionClassConstant对象的字符串表示形式
ReflectionClassConstant::__toString();string

ReflectionExtension类

报告了一个扩展(extension)的有关信息

创建对象

//构造一个ReflectionExtension 对象
ReflectionExtension::__construct(string $name)
  name 扩展的名称

//clone方法阻止实例克隆。反射类实例不允许被克隆
ReflectionExtension::__clone():void

获取类

//导出可以被反射的扩展。输出格式同CLI argument --re [extension].
ReflectionExtension::export(string $name , string $return = false):string
  name 导出的反射。
  return 设为 true 时返回导出结果,设为 false(默认值)则忽略返回

//从扩展中获取类列表
ReflectionExtension::getClasses():array

//获取类名称
ReflectionExtension::getClassNames():array

获取常量

//获取扩展中定义的常量
ReflectionExtension::getConstants():array

获取函数

//获取扩展中定义的函数
ReflectionExtension::getFunctions():array

其他信息

//返回一个以依赖的扩展名称为索引的数组,每一项的取值为 Required、Optional 或者 Conflicts
ReflectionExtension::getDependencies():array

//获取扩展在ini配置文件中的配置,数组索引是配置名称,值是配置值
ReflectionExtension::getINIEntries():array

//获取扩展名称
ReflectionExtension::getName():string

//获取扩展的版本号
ReflectionExtension::getVersion():string

//输出“phpinfo()”信息中的扩展信息
ReflectionExtension::info():void

//以string形式返回扩展的反射信息
ReflectionExtension::__toString():string

获取载入信息

//返回扩展是否持久化载入,扩展在extension配置中被载入返回true ,否则返回 false
ReflectionExtension::isPersistent():void

//返回扩展是否是临时载入,如果扩展被dl()载入则返回true ,否则返回 false 
ReflectionExtension::isTemporary():void

ReflectionFunctionAbstract类

获取函数信息

//复制函数
ReflectionFunctionAbstract::__clone():void

//返回本身的匿名函数
ReflectionFunctionAbstract::getClosureThis():object

//获取函数的扩展信息
ReflectionFunctionAbstract::getExtension():ReflectionExtension

//获取函数名称
ReflectionFunctionAbstract::getName():string

//获取函数的段名称 (没有命名空间定义)
ReflectionFunctionAbstract::getShortName():string

//获取反射函数的指定返回类型
ReflectionFunctionAbstract::getReturnType():ReflectionType|null

获取函数参数

//获取函数定义的参数数目,包括可选参数
ReflectionFunctionAbstract::getNumberOfParameters():int

//获取函数定义中,必须输入的参数个数
ReflectionFunctionAbstract::getNumberOfRequiredParameters():int

//通过 ReflectionParameter 数组返回参数列表
ReflectionFunctionAbstract::getParameters():array

获取文件信息

//获取函数定义的文件名称
ReflectionFunctionAbstract::getFileName():string

//获取函数定义的开始行号
ReflectionFunctionAbstract::getStartLine():int

//获取结束行号
ReflectionFunctionAbstract::getEndLine():int

获取其他信息

//获取静态变量
ReflectionFunctionAbstract::getStaticVariables():array

//返回与闭包关联的范围
ReflectionFunctionAbstract::getClosureScopeClass():ReflectionClass|null

//获取注释内容
ReflectionFunctionAbstract::getDocComment():string

//获取扩展名称
ReflectionFunctionAbstract::getExtensionName():string

//获取类定义所属的命名空间
ReflectionFunctionAbstract::getNamespaceName():string

ReflectionFunctionAbstract::__toString():void

检查定义

//检查函数是否在命名空间内定义
ReflectionFunctionAbstract::inNamespace():bool

//检查函数是否是用户自定义,也就是说不是 PHP 自己内置函数
ReflectionFunctionAbstract::isUserDefined():bool

//判断函数是否是内置函数,与用户自定义函数相反
ReflectionFunctionAbstract::isInternal():bool

//判断函数是否是一个生成器函数
ReflectionFunctionAbstract::isGenerator():bool

//检查是否是匿名函数
ReflectionFunctionAbstract::isClosure():bool

检查

//检查反射函数是否指定了返回类型
ReflectionFunctionAbstract::hasReturnType():bool

//检查是否已经弃用
ReflectionFunctionAbstract::isDeprecated():bool

//检查函数是否为可变参数。
ReflectionFunctionAbstract::isVariadic():bool

//检查是否返回参考信息
ReflectionFunctionAbstract::returnsReference():bool

ReflectionFunction类

报告了一个函数的有关信息,继承ReflectionFunctionAbstract类

常量

//指示了不建议使用的函数
ReflectionFunction::IS_DEPRECATED

方法

创建对象

//构造一个ReflectionFunction对象
ReflectionFunction::__construct(Closure|string $function)
  function 要反映的函数名或闭包

ReflectionFunction::__toString():string

获取创建的闭包

//返回为函数动态创建的闭包
ReflectionFunction::getClosure():Closure

反射调用

//反射调用函数
ReflectionFunction::invoke(mixed ...$args):void
  args 传入的参数列表。它接受可变数量的参数,这些参数很像call_user_func()一样传递给函数 。

//调用该函数并将其参数作为数组传递
ReflectionFunction::invokeArgs(array $args):mixed
  args 作为数组传递给函数的参数,就像 call_user_func_array()一样工作

ReflectionMethod类

报告了一个方法的有关信息,继承自ReflectionFunctionAbstract

常量

//方法是静态(static)的。
ReflectionMethod::IS_STATIC

//方法是 public 的。
ReflectionMethod::IS_PUBLIC

//方法是 protected 的。
ReflectionMethod::IS_PROTECTED

//方法是 private 的。
ReflectionMethod::IS_PRIVATE

//方法是 abstract 的。
ReflectionMethod::IS_ABSTRACT

//方法是 final 的。
ReflectionMethod::IS_FINAL

方法

创建对象

//构造一个新的ReflectionMethod
ReflectionMethod::__construct(mixed $class, string $name)
ReflectionMethod::__construct(string $class_method)
  class 包含方法的类名称或者这个类的一个实例
  name 方法的名称
  class_method 类名称和方法名称,之间使用::分隔

//返回反射方法对象的字符串表达
ReflectionMethod::__toString():string

修饰符

//返回一个方法的修饰符,返回值是一个代表常量的int值
ReflectionMethod::getModifiers():int

原型

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

回调

//输出一个回调方法
ReflectionMethod::export(string $class , string $name , bool $return = false):string
  class 类名称
  name 方法名称
  return 设为 true 时返回导出结果,设为 false(默认值)则忽略返回

//返回一个动态建立的方法调用接口,译者注:可以使用这个返回值直接调用非公开方法。
ReflectionMethod::getClosure(object $object):Closure
  object 不可以使用静态方法,需要其他类型的方法

获取反射实例

//获取被反射的方法所在的类的反射实例
ReflectionMethod::getDeclaringClass():ReflectionClass

执行反射

//执行一个反射的方法
ReflectionMethod::invoke(object $object , mixed $parameter = ? , mixed $... = ?):mixed
  object 如果执行的方法是静态类,那么这个参数传送 null。
  parameter 0,或者传送给方法的参数列表。可以通过这个参数,给方法传送大量的参数。

//使用数组给方法传送参数,并执行他。
ReflectionMethod::invokeArgs(object $object , array $args):mixed
  object 调用方法的对象,如果是静态对象,设置为 null
  args 使用 array 传送的方法参数。

验证类的方法

//判断方法是否是抽象方法
ReflectionMethod::isAbstract():bool

//判断方法是否是构造方法
ReflectionMethod::isConstructor():bool

//判断方法是否是析构方法
ReflectionMethod::isDestructor():bool

//判断方法是否定义 final
ReflectionMethod::isFinal():bool

//判断方法是否是私有方法
ReflectionMethod::isPrivate():bool

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

//判断方法是否是公开方法
ReflectionMethod::isPublic():bool

//判断方法是否是静态方法
ReflectionMethod::isStatic():bool

//设置方法是否可以访问,例如通过设置可以访问能够执行私有方法和保护方法
ReflectionMethod::setAccessible(bool $accessible):void
  accessible 可以访问设置 true,否则设置 false

ReflectionParameter类

取回了函数或方法参数的相关信息

创建实例

//构造一个ReflectionParameter实例
ReflectionParameter::__construct(string|array|object $function , int|string $param)
  function 反映参数的功能。
  param 指定参数位置 的int(从零开始),或者参数名称为string

ReflectionParameter::__clone():void

获取类

//获取声明的类
ReflectionParameter::getDeclaringClass():ReflectionClass|null

获取函数

//获取声明函数
ReflectionParameter::getDeclaringFunction():ReflectionFunctionAbstract

获取参数

//获取任何用户定义的或内部函数或方法的参数的默认值。如果参数不是可选的, 则将引发ReflectionException
ReflectionParameter::getDefaultValue():mixed

//如果默认值是常量或null,则返回任何用户定义的或内部函数或方法的参数的默认值的常量名称。如果参数不是可选的, 则将引发ReflectionException。
ReflectionParameter::getDefaultValueConstantName():string|null

//获取参数的名称
ReflectionParameter::getName():string

//获取参数的位置
ReflectionParameter::getPosition():int

//获取参数的类型提示类,类型为ReflectionClass对象。
ReflectionParameter::getClass():ReflectionClass

//获取参数的关联类型
ReflectionParameter::getType():ReflectionType|null

检查

//检查参数是否允许null。
ReflectionParameter::allowsNull():bool

//返回此参数是否可以按值传递
ReflectionParameter::canBePassedByValue():bool

//检查参数是否有默认值
ReflectionParameter::isDefaultValueAvailable():bool

//返回此参数的默认值是否为常数
ReflectionParameter::isDefaultValueConstant():bool

//检查参数是否可选
ReflectionParameter::isOptional():bool

//检查参数是否通过引用传递
ReflectionParameter::isPassedByReference():bool

//检查参数是否声明为 可变参数
ReflectionParameter::isVariadic():bool

//检查参数是否具有与之关联的类型。
ReflectionParameter::hasType():bool

ReflectionParameter::__toString():string

ReflectionProperty类

报告了类的属性的相关信息

常量

//指示了 static 的属性。
ReflectionProperty::IS_STATIC

//指示了 public 的属性。
ReflectionProperty::IS_PUBLIC

//指示了 protected 的属性。
ReflectionProperty::IS_PROTECTED

//指示了 private 的属性。
ReflectionProperty::IS_PRIVATE

方法

创建实例

//构造一个ReflectionProperty对象
ReflectionProperty::__construct(object|string $class , string $property)
  class 包含要反映的类名称的字符串或对象。
  property 被反映的属性的名称

ReflectionProperty::__clone():void

ReflectionProperty::__toString():string

获取类

//获取声明的类
ReflectionProperty::getDeclaringClass():ReflectionClass

属性

//获取属性的隐式或显式声明的默认值
ReflectionProperty::getDefaultValue():mixed

//获取属性的文档注释
ReflectionProperty::getDocComment():string|false

//获取属性修饰符
ReflectionProperty::getModifiers():int

//获取属性名称
ReflectionProperty::getName():string

//获取属性的关联类型
ReflectionProperty::getType():ReflectionType|null

//获取属性的值
ReflectionProperty::getValue(object|null $object = null):mixed
  object 如果该属性是非静态的,则必须提供一个对象以从中获取该属性。如果要在不提供对象的情况下获取默认属性,请使用ReflectionClass :: getDefaultProperties() 代替

//导出反射
ReflectionProperty::export(mixed $class , string $name , bool $return = ?):string
  argument 导出的反射。
  name 属性名称
  return 设为 true 时返回导出结果,设为 false(默认值)则忽略返回。

设置属性

//允许通过ReflectionProperty::getValue()和 ReflectionProperty::setValue()方法访问受保护的或私有的属性。
ReflectionProperty::setAccessible(bool $accessible):void
  accessible true以允许访问,或false

//设置(更改)属性的值
ReflectionProperty::setValue(object $object , mixed $value):void
ReflectionProperty::setValue(mixed $value):void
  object 如果该属性是非静态的,则必须提供一个对象以更改该属性。如果属性是静态的,则忽略此参数,仅value需要提供。
  value 新的价值

检查

//检查该属性是否为私有
ReflectionProperty::isPrivate():bool

//检查属性是否受保护
ReflectionProperty::isProtected():bool

//检查该属性是否为公共
ReflectionProperty::isPublic():bool

//检查属性是否为静态
ReflectionProperty::isStatic():bool

//检查属性是否已初始化
ReflectionProperty::isInitialized(object|null $object = null):bool
  object 如果该属性是非静态的,则必须提供一个对象以从中获取该属性

//检查属性是否具有与其关联的类型
ReflectionProperty::hasType():bool

//检查属性是否用默认值声明,包括隐式 null默认值。仅返回false没有默认值(或动态属性)的类型化属性
ReflectionProperty::hasDefaultValue():bool

//检查是否属性是默认属性,检查属性是在编译时声明的,还是在运行时动态声明的
ReflectionProperty::isDefault():bool

ReflectionType类

用于获取函数、类方法的参数或者返回值的类型

//检查参数是否允许null
ReflectionType::allowsNull():bool

ReflectionGenerator类

用于获取生成器的信息

创建对象

ReflectionGenerator::__construct(Generator $generator)
  generator 生成器对象

//获取执行中的Generator对象
ReflectionGenerator::getExecutingGenerator():Generator

获取文件信息

//获取当前正在执行的生成器的完整路径和文件名
ReflectionGenerator::getExecutingFile():string

//获取生成器当前正在执行的行号
ReflectionGenerator::getExecutingLine():int

获取其他信息

//允许通过返回从ReflectionFunctionAbstract派生的类来获取生成器的函数名称
ReflectionGenerator::getFunction():ReflectionFunctionAbstract

//获取$this生成器可以访问的值。
ReflectionGenerator::getThis():object|null

//获取当前正在执行的生成器的跟踪
ReflectionGenerator::getTrace(int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT):array
  options 
    DEBUG_BACKTRACE_PROVIDE_OBJECT 默认
    DEBUG_BACKTRACE_IGNORE_ARGS 不要在堆栈跟踪中包含函数的参数信息。

Reflector接口

被所有可导出的反射类所实现(implement)

Reflector::export():string
Reflector::__toString():string

ReflectionException类

 

{/if}