PHP反射使用執行個體和PHP反射API的中文說明_PHP教程

來源:互聯網
上載者:User
最近在開發過程中需要擷取某個類方法的參數數量、名稱及參數順序,好根據參數的名稱來從$_GET裡取值。

如方法原型為test($uid,$score), 那麼我就知道需要需要從$_GET取
複製代碼 代碼如下:
$uid = $_GET['uid'];

$score = $_GET['score'];

然後調用方法$obj->test($uid,$score)

當然前提是約定好了參數名稱和get方法傳值變數名一致。

採用PHP的反射API,獲得函數參數名稱和參數預設值的方法如下:
複製代碼 代碼如下:
<?php
class testClass{

public function testFunc($param1,$param2=0){

}
}

$method = new ReflectionMethod('testClass', 'testFunc');
$params = $method--->getParameters();
foreach ($params as $param) {
echo 'param name: ' . $param->getName(),"\n";
if ($param->isOptional()) {
echo 'Default value: ' . $param->getDefaultValue(),"\n";
}
}

下面是PHP反射API的介紹:

1、用途:
該擴充分析php程式,匯出或提取出關於類、方法、屬性、參數等的詳細資料,包括注釋。
Reflection可以說是對php庫函數:“Classes/Objects 類/對象函數”的一個擴充。
主要用在通過程式檢測現有php程式內部關於類、方法等資訊,並做出處理。

2、API概覽:
複製代碼 代碼如下:
class Reflection { }
interface Reflector { }
class ReflectionException extends Exception { }
class ReflectionFunction implements Reflector { }
class ReflectionParameter implements Reflector { }
class ReflectionMethod extends ReflectionFunction { }
class ReflectionClass implements Reflector { }
class ReflectionObject extends ReflectionClass { }
class ReflectionProperty implements Reflector { }
class ReflectionExtension implements Reflector { }

3、詳細說明:(例子詳見php手冊)
複製代碼 代碼如下:
①Reflection類
<?php
class Reflection
{
public static mixed export(Reflector r [,bool return])
//匯出一個類或方法的詳細資料
public static array getModifierNames(int modifiers)
//取得修飾符的名字
}
?>

②ReflectionException類

該類繼承標準類,沒特殊方法和屬性。

③ReflectionFunction類
<?php
class ReflectionFunction implements Reflector
{
final private __clone()
public object __construct(string name)
public string __toString()
public static string export()
//匯出該函數的詳細資料
public string getName()
//取得函數名
public bool isInternal()
//測試是否為系統內建函式
public bool isUserDefined()
//測試是否為使用者自訂函數
public string getFileName()
//取得檔案名稱,包括路徑名
public int getStartLine()
//取得定義函數的起始行
public int getEndLine()
//取得定義函數的結束行
public string getDocComment()
//取得函數的注釋
public array getStaticVariables()
//取得靜態變數
public mixed invoke(mixed* args)
//調用該函數,通過參數列表傳參數
public mixed invokeArgs(array args)
//調用該函數,通過數組傳參數
public bool returnsReference()
//測試該函數是否返回引用
public ReflectionParameter[] getParameters()
//取得該方法所需的參數,傳回值為對象數組
public int getNumberOfParameters()
//取得該方法所需的參數個數
public int getNumberOfRequiredParameters()
//取得該方法所需的參數個數
}
?>

④ReflectionParameter類:
<?php
class ReflectionParameter implements Reflector
{
final private __clone()
public object __construct(string name)
public string __toString()
public static string export()
//匯出該參數的詳細資料
public string getName()
//取得參數名
public bool isPassedByReference()
//測試該參數是否通過引用傳遞參數
public ReflectionClass getClass()
//若該參數為對象,返回該對象的類名
public bool isArray()
//測試該參數是否為數群組類型
public bool allowsNull()
//測試該參數是否允許為空白
public bool isOptional()
//測試該參數是否為可選的,當有預設參數時可選
public bool isDefaultValueAvailable()
//測試該參數是否為預設參數
public mixed getDefaultValue()
//取得該參數的預設值
}
?>

⑤ReflectionClass類:
<?php
class ReflectionClass implements Reflector
{
final private __clone()
public object __construct(string name)
public string __toString()
public static string export()
//匯出該類的詳細資料
public string getName()
//取得類名或介面名
public bool isInternal()
//測試該類是否為系統內部類
public bool isUserDefined()
//測試該類是否為使用者自訂類
public bool isInstantiable()
//測試該類是否被執行個體化過
public bool hasConstant(string name)
//測試該類是否有特定的常量
public bool hasMethod(string name)
//測試該類是否有特定的方法
public bool hasProperty(string name)
//測試該類是否有特定的屬性
public string getFileName()
//取得定義該類的檔案名稱,包括路徑名
public int getStartLine()
//取得定義該類的開始行
public int getEndLine()
//取得定義該類的結束行
public string getDocComment()
//取得該類的注釋
public ReflectionMethod getConstructor()
//取得該類的建構函式資訊
public ReflectionMethod getMethod(string name)
//取得該類的某個特定的方法資訊
public ReflectionMethod[] getMethods()
//取得該類的所有的方法資訊
public ReflectionProperty getProperty(string name)
//取得某個特定的屬性資訊
public ReflectionProperty[] getProperties()
//取得該類的所有屬性資訊
public array getConstants()
//取得該類所有常量資訊
public mixed getConstant(string name)
//取得該類特定常量資訊
public ReflectionClass[] getInterfaces()
//取得介面類資訊
public bool isInterface()
//測試該類是否為介面
public bool isAbstract()
//測試該類是否為抽象類別
public bool isFinal()
//測試該類是否聲明為final
public int getModifiers()
//取得該類的修飾符,傳回值類型可能是個資源類型
//通過Reflection::getModifierNames($class->getModifiers())進一步讀取
public bool isInstance(stdclass object)
//測試傳入的對象是否為該類的一個執行個體
public stdclass newInstance(mixed* args)
//建立該類執行個體
public ReflectionClass getParentClass()
//取得父類
public bool isSubclassOf(ReflectionClass class)
//測試傳入的類是否為該類的父類
public array getStaticProperties()
//取得該類的所有靜態屬性
public mixed getStaticPropertyValue(string name [, mixed default])
//取得該類的靜態屬性值,若private,則不可訪問
public void setStaticPropertyValue(string name, mixed value)
//設定該類的靜態屬性值,若private,則不可訪問,有悖封裝原則
public array getDefaultProperties()
//取得該類的屬性資訊,不含靜態屬性
public bool isIterateable()
public bool implementsInterface(string name)
//測試是否實現了某個特定介面
public ReflectionExtension getExtension()
public string getExtensionName()
}
?>

⑥ReflectionMethod類:
<?php
class ReflectionMethod extends ReflectionFunction
{
public __construct(mixed class, string name)
public string __toString()
public static string export()
//匯出該方法的資訊
public mixed invoke(stdclass object, mixed* args)
//調用該方法
public mixed invokeArgs(stdclass object, array args)
//調用該方法,傳多參數
public bool isFinal()
//測試該方法是否為final
public bool isAbstract()
//測試該方法是否為abstract
public bool isPublic()
//測試該方法是否為public
public bool isPrivate()
//測試該方法是否為private
public bool isProtected()
//測試該方法是否為protected
public bool isStatic()
//測試該方法是否為static
public bool isConstructor()
//測試該方法是否為建構函式
public bool isDestructor()
//測試該方法是否為解構函式
public int getModifiers()
//取得該方法的修飾符
public ReflectionClass getDeclaringClass()
//取得該方法所屬的類
// Inherited from ReflectionFunction
final private __clone()
public string getName()
public bool isInternal()
public bool isUserDefined()
public string getFileName()
public int getStartLine()
public int getEndLine()
public string getDocComment()
public array getStaticVariables()
public bool returnsReference()
public ReflectionParameter[] getParameters()
public int getNumberOfParameters()
public int getNumberOfRequiredParameters()
}
?>

⑦ReflectionProperty類:
<?php
class ReflectionProperty implements Reflector
{
final private __clone()
public __construct(mixed class, string name)
public string __toString()
public static string export()
//匯出該屬性的詳細資料
public string getName()
//取得該屬性名稱
public bool isPublic()
//測試該屬性名稱是否為public
public bool isPrivate()
//測試該屬性名稱是否為private
public bool isProtected()
//測試該屬性名稱是否為protected
public bool isStatic()
//測試該屬性名稱是否為static
public bool isDefault()
public int getModifiers()
//取得修飾符
public mixed getValue(stdclass object)
//取得該屬性值
public void setValue(stdclass object, mixed value)
//設定該屬性值
public ReflectionClass getDeclaringClass()
//取得定義該屬性的類
public string getDocComment()
//取得該屬性的注釋
}
?>

⑧ReflectionExtension類
<?php
class ReflectionExtension implements Reflector {
final private __clone()
public __construct(string name)
public string __toString()

public static string export()
//匯出該擴充的所有資訊
public string getName()
//取得該擴充的名字
public string getVersion()
//取得該擴充的版本
public ReflectionFunction[] getFunctions()
//取得該擴充的所有函數
public array getConstants()
//取得該擴充的所有常量
public array getINIEntries()
//取得與該擴充相關的,在php.ini中的指令資訊
public ReflectionClass[] getClasses()
public array getClassNames()
}
?>

http://www.bkjia.com/PHPjc/824666.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/824666.htmlTechArticle最近在開發過程中需要擷取某個類方法的參數數量、名稱及參數順序,好根據參數的名稱來從$_GET裡取值。 如方法原型為test($uid,$score), 那麼...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.