最近開發支付寶相關功能的時候,因爲支付寶的SDK比較落伍,不支持composer的方式加載,使用三方的composer SDK又以爲不放心php
爲了簡化代碼的調用方式,使用PHP的反射類針對支付寶官方SDK作了一層封裝,使開發中僅須要關心業務層便可,理論上實現了支付寶SDK全功能反射服務api
有須要的的同窗能夠安裝體驗一下:數組
composer require jiujiude/alipay-sdk
下面整理了一些反射類相關的知識composer
反射是什麼?框架
它是指在PHP運行狀態中,擴展分析PHP程序,導出或提取出關於類、方法、屬性、參數等的詳細信息,包括註釋。函數
這種動態獲取的信息以及動態調用對象的方法的功能稱爲反射API。ui
反射是操縱面向對象範型中元模型的API,其功能十分強大,可幫助咱們構建複雜,可擴展的應用。this
能夠幹什麼?spa
能夠作自動加載插件,自動生成文檔,甚至可用來擴充PHP語言,可謂十分強大。插件
<?php class Person { /** * For the sake of demonstration, we"re setting this private */ private $_allowDynamicAttributes = false; /** * type=primary_autoincrement */ protected $id = 0; /** * type=varchar length=255 null */ protected $name; /** * type=text null */ protected $biography; public function getId() { return $this->id; } public function setId($v) { $this->id = $v; } public function getName() { return $this->name; } public function setName($v) { $this->name = $v; } public function getBiography() { return $this->biography; } public function setBiography($v) { $this->biography = $v; } }
1、經過ReflectionClass,咱們能夠獲得Person類的如下信息:
接下來反射它,只要把類名"Person"傳遞給ReflectionClass就能夠了
$class = new ReflectionClass('Person'); // 創建 Person這個類的反射類 $instance = $class->newInstanceArgs($args); // 至關於實例化Person 類
1)獲取屬性(Properties)
$properties = $class->getProperties(); foreach ($properties as $property) { echo $property->getName() . "\n"; } // 輸出: // _allowDynamicAttributes // id // name // biography
默認狀況下,ReflectionClass會獲取到全部的屬性,private 和 protected的也能夠。若是隻想獲取到private屬性,就要額外傳個參數:
$private_properties = $class->getProperties(ReflectionProperty::IS_PRIVATE);
可用參數列表:
ReflectionProperty::IS_STATIC ReflectionProperty::IS_PUBLIC ReflectionProperty::IS_PROTECTED ReflectionProperty::IS_PRIVATE
2)獲取註釋
經過getDocComment能夠獲得寫給property的註釋。
foreach ($properties as $property) { if ($property->isProtected()) { $docblock = $property->getDocComment(); preg_match('/ type\=([a-z_]*) /', $property->getDocComment(), $matches); echo $matches[1] . "\n"; } } // Output: // primary_autoincrement // varchar // text
3)獲取類的方法
getMethods() 來獲取到類的全部methods。 hasMethod(string) 是否存在某個方法 getMethod(string) 獲取方法
4)執行類的方法
$instance->getName(); // 執行Person 裏的方法getName // 或者: $method = $class->getmethod('getName'); // 獲取Person 類中的getName方法 $method->invoke($instance); // 執行getName 方法 // 或者: $method = $class->getmethod('setName'); // 獲取Person 類中的setName方法 $method->invokeArgs($instance, array('xxxx.com'));
2、經過ReflectionMethod,咱們能夠獲得Person類的某個方法的信息:
// 執行detail方法 $method = new ReflectionMethod('Person', 'test'); if ($method->isPublic() && !$method->isStatic()) { echo 'Action is right'; } echo $method->getNumberOfParameters(); // 參數個數 echo $method->getParameters(); // 參數對象數組
固然還有不少的功能和方法這裏就不一一贅述了,你們能夠去官方文檔查看,發揮本身的想象力~,拋磚引玉~~
附錄:
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::isIterable — Check whether this class is iterable ReflectionClass::isIterateable — 檢查是否可迭代(iterateable) ReflectionClass::isSubclassOf — 檢查是否爲一個子類 ReflectionClass::isTrait — 返回了是否爲一個 trait ReflectionClass::isUserDefined — 檢查是否由用戶定義的 ReflectionClass::newInstance — 從指定的參數建立一個新的類實例 ReflectionClass::newInstanceArgs — 從給出的參數建立一個新的類實例。 ReflectionClass::newInstanceWithoutConstructor — 建立一個新的類實例而不調用它的構造函數 ReflectionClass::setStaticPropertyValue — 設置靜態屬性的值 ReflectionClass::__toString — 返回 ReflectionClass 對象字符串的表示形式。
做者:舊舊的 <393210556@qq.com> 解決問題的方式,就是解決它一次