反射php,php的反射 · thinkphp5.0 源码阅读学习 · 看云

[TOC]

* * * * *

## 1 反射的意义

php中的类,接口,函数,方法,扩展都是以**结构体对象与名称**关联的

反射API可以**通过名称对其所关联的结构体对象的属性进行操作**。

反射API可操作结构体对象**包括扩展 类 接口 函数 方法**等。

## 2 反射简介

反射API的整体类结构如下

Reflector ;反射接口,反射类需要实现的功能

Reflection ;基础反射操作类

ReflectionZendExtension ;Zend引擎扩展反射类

ReflectionExtension ;普通扩展反射类

ReflectionFunctionAbstract ;方法和函数的抽象反射类

ReflectionFunction ;函数反射类

ReflectionParameter ;方法和函数的参数反射类型

ReflectionClass ;类反射类

ReflectionObject ;对象反射类

ReflectionMethod ;类方法反射类

ReflctionProperty ;类属性反射类

## 3 类的反射类 ReflectionCLass

~~~

ReflectionClass implements Reflector {

/* 常量 */

const integer IS_IMPLICIT_ABSTRACT = 16 ;

const integer IS_EXPLICIT_ABSTRACT = 32 ;

const integer IS_FINAL = 64 ;

/* 属性 */

public $name ;

/* 方法 */

public __construct ( mixed $argument )

public static string export ( mixed $argument [, bool $return = false ] )

public mixed getConstant ( string $name )

public array getConstants ( void )

public ReflectionMethod getConstructor ( void )

public array getDefaultProperties ( void )

public string getDocComment ( void )

public int getEndLine ( void )

public ReflectionExtension getExtension ( void )

public string getExtensionName ( void )

public string getFileName ( void )

public array getInterfaceNames ( void )

public array getInterfaces ( void )

public ReflectionMethod getMethod ( string $name )

public array getMethods ([ int $filter ] )

public int getModifiers ( void )

public string getName ( void )

public string getNamespaceName ( void )

public object getParentClass ( void )

public array getProperties ([ int $filter ] )

public ReflectionProperty getProperty ( string $name )

public string getShortName ( void )

public int getStartLine ( void )

public array getStaticProperties ( void )

public mixed getStaticPropertyValue ( string $name [, mixed &$def_value ] )

public array getTraitAliases ( void )

public array getTraitNames ( void )

public array getTraits ( void )

public bool hasConstant ( string $name )

public bool hasMethod ( string $name )

public bool hasProperty ( string $name )

public bool implementsInterface ( string $interface )

public bool inNamespace ( void )

public bool isAbstract ( void )

public bool isCloneable ( void )

public bool isFinal ( void )

public bool isInstance ( object $object )

public bool isInstantiable ( void )

public bool isInterface ( void )

public bool isInternal ( void )

public bool isIterateable ( void )

public bool isSubclassOf ( string $class )

public bool isTrait ( void )

public bool isUserDefined ( void )

public object newInstance ( mixed $args [, mixed $... ] )

public object newInstanceArgs ([ array $args ] )

public object newInstanceWithoutConstructor ( void )

public void setStaticPropertyValue ( string $name , string $value )

public string __toString ( void )

}

~~~

>[info] 属性: 类的名称,

` name 只读的,不可赋值`

>[info] 常量: ReflectionClass 修饰符

~~~

ReflectionClass::IS_IMPLICIT_ABSTRACT

指示了类是一个抽象类(abstract)因为它有抽象(abstract)方法

ReflectionClass::IS_EXPLICIT_ABSTRACT

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

ReflectionClass::IS_FINAL

指示这是一个 final 类。

~~~

>[info] 方法

**反射基础操作**

~~~

ReflectionClass::__construct — 构造一个 ReflectionClass 类

ReflectionClass::export — 导出一个类

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

~~~

**外在环境信息**

~~~

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

ReflectionClass::getStartLine — 获取起始行号

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

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

ReflectionClass::getExtension — 获取所在扩展的 ReflectionExtension 对象

~~~

**内置属性信息**

~~~

ReflectionClass::getModifiers — 获取修饰符

ReflectionClass::getName — 获取类名

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

ReflectionClass::getShortName — 获取短名

ReflectionClass::getParentClass — 获取父类

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

ReflectionClass::getInterfaces — 获取接口

ReflectionClass::getConstants — 获取一组常量

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

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

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

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

ReflectionClass::getProperties — 获取一组属性

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

ReflectionClass::getDefaultProperties — 获取默认属性

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

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

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

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

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

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

~~~

**判断性操作**

~~~

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

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

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

ReflectionClass::implementsInterface — 接口的实现

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

ReflectionClass::isInstance — 检查类的实例

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

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

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

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

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

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

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

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

~~~

**创建性操作**

~~~

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

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

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

~~~

## 4 对象的反射类 ReflectionObject

~~~

ReflectionObject extends ReflectionClass implements Reflector {

/* 常量 */

const integer IS_IMPLICIT_ABSTRACT = 16 ;

const integer IS_EXPLICIT_ABSTRACT = 32 ;

const integer IS_FINAL = 64 ;

/* 属性 */

public $name ;

/* 方法 */

public __construct ( object $argument )

public static string export ( string $argument [, bool $return ] )

/* 继承的方法 */

public ReflectionClass::__construct ( mixed $argument )

public static string ReflectionClass::export ( mixed $argument [, bool $return = false ] )

public mixed ReflectionClass::getConstant ( string $name )

public array ReflectionClass::getConstants ( void )

public ReflectionMethod ReflectionClass::getConstructor ( void )

public array ReflectionClass::getDefaultProperties ( void )

public string ReflectionClass::getDocComment ( void )

public int ReflectionClass::getEndLine ( void )

public ReflectionExtension ReflectionClass::getExtension ( void )

public string ReflectionClass::getExtensionName ( void )

public string ReflectionClass::getFileName ( void )

public array ReflectionClass::getInterfaceNames ( void )

public array ReflectionClass::getInterfaces ( void )

public ReflectionMethod ReflectionClass::getMethod ( string $name )

public array ReflectionClass::getMethods ([ int $filter ] )

public int ReflectionClass::getModifiers ( void )

public string ReflectionClass::getName ( void )

public string ReflectionClass::getNamespaceName ( void )

public object ReflectionClass::getParentClass ( void )

public array ReflectionClass::getProperties ([ int $filter ] )

public ReflectionProperty ReflectionClass::getProperty ( string $name )

public string ReflectionClass::getShortName ( void )

public int ReflectionClass::getStartLine ( void )

public array ReflectionClass::getStaticProperties ( void )

public mixed ReflectionClass::getStaticPropertyValue ( string $name [, mixed &$def_value ] )

public array ReflectionClass::getTraitAliases ( void )

public array ReflectionClass::getTraitNames ( void )

public array ReflectionClass::getTraits ( void )

public bool ReflectionClass::hasConstant ( string $name )

public bool ReflectionClass::hasMethod ( string $name )

public bool ReflectionClass::hasProperty ( string $name )

public bool ReflectionClass::implementsInterface ( string $interface )

public bool ReflectionClass::inNamespace ( void )

public bool ReflectionClass::isAbstract ( void )

public bool ReflectionClass::isCloneable ( void )

public bool ReflectionClass::isFinal ( void )

public bool ReflectionClass::isInstance ( object $object )

public bool ReflectionClass::isInstantiable ( void )

public bool ReflectionClass::isInterface ( void )

public bool ReflectionClass::isInternal ( void )

public bool ReflectionClass::isIterateable ( void )

public bool ReflectionClass::isSubclassOf ( string $class )

public bool ReflectionClass::isTrait ( void )

public bool ReflectionClass::isUserDefined ( void )

public object ReflectionClass::newInstance ( mixed $args [, mixed $... ] )

public object ReflectionClass::newInstanceArgs ([ array $args ] )

public object ReflectionClass::newInstanceWithoutConstructor ( void )

public void ReflectionClass::setStaticPropertyValue ( string $name , string $value )

public string ReflectionClass::__toString ( void )

}

~~~

> 属性 对象的类名

` name 只读`

> 方法 (重写)

~~~

ReflectionObject::__construct — 构造对象

ReflectionObject ReflectionObject::export — 导出对象

~~~

其他常量,方法继承 ReflectionClass类的静态方法

* * * * *

## 5 方法的反射类 ReflectionMethod

~~~

ReflectionMethod extends ReflectionFunctionAbstract implements Reflector {

/* 常量 */

const integer IS_STATIC = 1 ;

const integer IS_PUBLIC = 256 ;

const integer IS_PROTECTED = 512 ;

const integer IS_PRIVATE = 1024 ;

const integer IS_ABSTRACT = 2 ;

const integer IS_FINAL = 4 ;

/* 属性 */

public $name ;

public $class ;

/* 方法 */

public __construct ( mixed $class , string $name )

public static string export ( string $class , string $name [, bool $return = false ] )

public Closure getClosure ( object $object )

public ReflectionClass getDeclaringClass ( void )

public int getModifiers ( void )

public ReflectionMethod getPrototype ( void )

public mixed invoke ( object $object [, mixed $parameter [, mixed $... ]] )

public mixed invokeArgs ( object $object , array $args )

public bool isAbstract ( void )

public bool isConstructor ( void )

public bool isDestructor ( void )

public bool isFinal ( void )

public bool isPrivate ( void )

public bool isProtected ( void )

public bool isPublic ( void )

public bool isStatic ( void )

public void setAccessible ( bool $accessible )

public string __toString ( void )

/* 继承的方法 */

final private void ReflectionFunctionAbstract::__clone ( void )

public ReflectionClass ReflectionFunctionAbstract::getClosureScopeClass ( void )

public object ReflectionFunctionAbstract::getClosureThis ( void )

public string ReflectionFunctionAbstract::getDocComment ( void )

public int ReflectionFunctionAbstract::getEndLine ( void )

public ReflectionExtension ReflectionFunctionAbstract::getExtension ( void )

public string ReflectionFunctionAbstract::getExtensionName ( void )

public string ReflectionFunctionAbstract::getFileName ( void )

public string ReflectionFunctionAbstract::getName ( void )

public string ReflectionFunctionAbstract::getNamespaceName ( void )

public int ReflectionFunctionAbstract::getNumberOfParameters ( void )

public int ReflectionFunctionAbstract::getNumberOfRequiredParameters ( void )

public array ReflectionFunctionAbstract::getParameters ( void )

public string ReflectionFunctionAbstract::getShortName ( void )

public int ReflectionFunctionAbstract::getStartLine ( void )

public array ReflectionFunctionAbstract::getStaticVariables ( void )

public bool ReflectionFunctionAbstract::inNamespace ( void )

public bool ReflectionFunctionAbstract::isClosure ( void )

public bool ReflectionFunctionAbstract::isDeprecated ( void )

public bool ReflectionFunctionAbstract::isGenerator ( void )

public bool ReflectionFunctionAbstract::isInternal ( void )

public bool ReflectionFunctionAbstract::isUserDefined ( void )

public bool ReflectionFunctionAbstract::isVariadic ( void )

public bool ReflectionFunctionAbstract::returnsReference ( void )

abstract public void ReflectionFunctionAbstract::__toString ( void )

}

~~~

> 属性

~~~

name 方法名称

~~~

~~~

class 类名称

~~~

> 预定义常量

~~~

ReflectionMethod::IS_STATIC

指示一个方法是静态(static)的。

ReflectionMethod::IS_PUBLIC

指示一个方法是 public 的。

ReflectionMethod::IS_PROTECTED

指示一个方法是 protected 的。

ReflectionMethod::IS_PRIVATE

指示一个方法是 private 的。

ReflectionMethod::IS_ABSTRACT

指示一个方法是 abstract 的。

ReflectionMethod::IS_FINAL

指示一个方法是 final 的。

~~~

> 方法

**属性信息**

~~~

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::__construct — ReflectionMethod 的构造函数

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

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

~~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值