[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 — 返回反射方法對象的字符串表達
~~~
- 更新記錄
- 概述
- 文件索引
- 函數索引
- 章節格式
- 框架流程
- 前:章節說明
- 主:(index.php)入口
- 主:(start.php)框架引導
- 主:(App.php)應用啟動
- 主:(App.php)應用調度
- C:(Controller.php)應用控制器
- M:(Model.php)數據模型
- V:(View.php)視圖對象
- 附:(App.php)應用啟動
- 附:(base.php)全局變量
- 附:(common.php)模式配置
- 附:(convention.php)全局配置
- 附:(Loader.php)自動加載器
- 附:(Build.php)自動生成
- 附:(Hook.php)監聽回調
- 附:(Route.php)全局路由
- 附:(Response.php)數據輸出
- 附:(Log.php)日志記錄
- 附:(Exception.php)異常處理
- 框架工具
- 另:(helper.php)輔助函數
- 另:(Cache.php)數據緩存
- 另:(Cookie.php)cookie操作
- 另:(Console.php)控制臺
- 另:(Debug.php)開發調試
- 另:(Error.php)錯誤處理
- 另:(Url.php)Url操作文件
- 另:(Loader.php)加載器實例化
- 另:(Input.php)數據輸入
- 另:(Lang.php)語言包管理
- 另:(ORM.php)ORM基類
- 另:(Process.php)進程管理
- 另:(Session.php)session操作
- 另:(Template.php)模板解析
- 框架驅動
- D:(\config)配置解析
- D:(\controller)控制器擴展
- D:(\model)模型擴展
- D:(\db)數據庫驅動
- D:(\view)模板解析
- D:(\template)模板標簽庫
- D:(\session)session驅動
- D:(\cache)緩存驅動
- D:(\console)控制臺
- D:(\process)進程擴展
- T:(\traits)Trait目錄
- D:(\exception)異常實現
- D:(\log)日志驅動
- 使用范例
- 服務器與框架的安裝
- 控制器操作
- 數據模型操作
- 視圖渲染控制
- MVC開發初探
- 模塊開發
- 入口文件定義全局變量
- 運行模式開發
- 框架配置
- 自動生成應用
- 事件與插件注冊
- 路由規則注冊
- 輸出控制
- 多種應用組織
- 綜合應用
- tp框架整合后臺auto架構快速開發
- 基礎原理
- php默認全局變量
- php的魔術方法
- php命名空間
- php的自動加載
- php的composer
- php的反射
- php的trait機制
- php設計模式
- php的系統時區
- php的異常錯誤
- php的輸出控制
- php的正則表達式
- php的閉包函數
- php的會話控制
- php的接口
- php的PDO
- php的字符串操作
- php的curl
- 框架心得
- 心:整體結構
- 心:配置詳解
- 心:加載器詳解
- 心:輸入輸出詳解
- 心:url路由詳解
- 心:模板詳解
- 心:模型詳解
- 心:日志詳解
- 心:緩存詳解
- 心:控制臺詳解
- 框架更新
- 4.20(驗證類,助手函數)
- 4.27(新模型Model功能)
- 5.4(新數據庫驅動)
- 7.28(自動加載)