如何正確的使用PHP反射?針對這個(gè)問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡單易行的方法。
為漢陰等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及漢陰網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為網(wǎng)站建設(shè)、網(wǎng)站制作、漢陰網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!最近在開發(fā)過程中需要獲取某個(gè)類方法的參數(shù)數(shù)量、名稱及參數(shù)順序,好根據(jù)參數(shù)的名稱來從$_GET里取值。
如方法原型為test($uid,$score), 那么我就知道需要需要從$_GET取
復(fù)制代碼 代碼如下:
$uid = $_GET['uid'];
$score = $_GET['score'];
然后調(diào)用方法$obj->test($uid,$score)
當(dāng)然前提是約定好了參數(shù)名稱和get方法傳值變量名一致。
采用PHP的反射API,獲得函數(shù)參數(shù)名稱和參數(shù)默認(rèn)值的方法如下:
復(fù)制代碼 代碼如下:
<?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、用途:
該擴(kuò)展分析php程序,導(dǎo)出或提取出關(guān)于類、方法、屬性、參數(shù)等的詳細(xì)信息,包括注釋。
Reflection可以說是對php庫函數(shù):“Classes/Objects 類/對象函數(shù)”的一個(gè)擴(kuò)展。
主要用在通過程序檢測現(xiàn)有php程序內(nèi)部關(guān)于類、方法等信息,并做出處理。
2、API概覽:
復(fù)制代碼 代碼如下:
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、詳細(xì)說明:(例子詳見php手冊)
復(fù)制代碼 代碼如下:
①Reflection類
<?php
class Reflection
{
public static mixed export(Reflector r [,bool return])
//導(dǎo)出一個(gè)類或方法的詳細(xì)信息
public static array getModifierNames(int modifiers)
//取得修飾符的名字
}
?>
②ReflectionException類
該類繼承標(biāo)準(zhǔn)類,沒特殊方法和屬性。
③ReflectionFunction類
<?php
class ReflectionFunction implements Reflector
{
final private __clone()
public object __construct(string name)
public string __toString()
public static string export()
//導(dǎo)出該函數(shù)的詳細(xì)信息
public string getName()
//取得函數(shù)名
public bool isInternal()
//測試是否為系統(tǒng)內(nèi)部函數(shù)
public bool isUserDefined()
//測試是否為用戶自定義函數(shù)
public string getFileName()
//取得文件名,包括路徑名
public int getStartLine()
//取得定義函數(shù)的起始行
public int getEndLine()
//取得定義函數(shù)的結(jié)束行
public string getDocComment()
//取得函數(shù)的注釋
public array getStaticVariables()
//取得靜態(tài)變量
public mixed invoke(mixed* args)
//調(diào)用該函數(shù),通過參數(shù)列表傳參數(shù)
public mixed invokeArgs(array args)
//調(diào)用該函數(shù),通過數(shù)組傳參數(shù)
public bool returnsReference()
//測試該函數(shù)是否返回引用
public ReflectionParameter[] getParameters()
//取得該方法所需的參數(shù),返回值為對象數(shù)組
public int getNumberOfParameters()
//取得該方法所需的參數(shù)個(gè)數(shù)
public int getNumberOfRequiredParameters()
//取得該方法所需的參數(shù)個(gè)數(shù)
}
?>
④ReflectionParameter類:
<?php
class ReflectionParameter implements Reflector
{
final private __clone()
public object __construct(string name)
public string __toString()
public static string export()
//導(dǎo)出該參數(shù)的詳細(xì)信息
public string getName()
//取得參數(shù)名
public bool isPassedByReference()
//測試該參數(shù)是否通過引用傳遞參數(shù)
public ReflectionClass getClass()
//若該參數(shù)為對象,返回該對象的類名
public bool isArray()
//測試該參數(shù)是否為數(shù)組類型
public bool allowsNull()
//測試該參數(shù)是否允許為空
public bool isOptional()
//測試該參數(shù)是否為可選的,當(dāng)有默認(rèn)參數(shù)時(shí)可選
public bool isDefaultValueAvailable()
//測試該參數(shù)是否為默認(rèn)參數(shù)
public mixed getDefaultValue()
//取得該參數(shù)的默認(rèn)值
}
?>
⑤ReflectionClass類:
<?php
class ReflectionClass implements Reflector
{
final private __clone()
public object __construct(string name)
public string __toString()
public static string export()
//導(dǎo)出該類的詳細(xì)信息
public string getName()
//取得類名或接口名
public bool isInternal()
//測試該類是否為系統(tǒng)內(nèi)部類
public bool isUserDefined()
//測試該類是否為用戶自定義類
public bool isInstantiable()
//測試該類是否被實(shí)例化過
public bool hasConstant(string name)
//測試該類是否有特定的常量
public bool hasMethod(string name)
//測試該類是否有特定的方法
public bool hasProperty(string name)
//測試該類是否有特定的屬性
public string getFileName()
//取得定義該類的文件名,包括路徑名
public int getStartLine()
//取得定義該類的開始行
public int getEndLine()
//取得定義該類的結(jié)束行
public string getDocComment()
//取得該類的注釋
public ReflectionMethod getConstructor()
//取得該類的構(gòu)造函數(shù)信息
public ReflectionMethod getMethod(string name)
//取得該類的某個(gè)特定的方法信息
public ReflectionMethod[] getMethods()
//取得該類的所有的方法信息
public ReflectionProperty getProperty(string name)
//取得某個(gè)特定的屬性信息
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()
//取得該類的修飾符,返回值類型可能是個(gè)資源類型
//通過Reflection::getModifierNames($class->getModifiers())進(jìn)一步讀取
public bool isInstance(stdclass object)
//測試傳入的對象是否為該類的一個(gè)實(shí)例
public stdclass newInstance(mixed* args)
//創(chuàng)建該類實(shí)例
public ReflectionClass getParentClass()
//取得父類
public bool isSubclassOf(ReflectionClass class)
//測試傳入的類是否為該類的父類
public array getStaticProperties()
//取得該類的所有靜態(tài)屬性
public mixed getStaticPropertyValue(string name [, mixed default])
//取得該類的靜態(tài)屬性值,若private,則不可訪問
public void setStaticPropertyValue(string name, mixed value)
//設(shè)置該類的靜態(tài)屬性值,若private,則不可訪問,有悖封裝原則
public array getDefaultProperties()
//取得該類的屬性信息,不含靜態(tài)屬性
public bool isIterateable()
public bool implementsInterface(string name)
//測試是否實(shí)現(xiàn)了某個(gè)特定接口
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()
//導(dǎo)出該方法的信息
public mixed invoke(stdclass object, mixed* args)
//調(diào)用該方法
public mixed invokeArgs(stdclass object, array args)
//調(diào)用該方法,傳多參數(shù)
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()
//測試該方法是否為構(gòu)造函數(shù)
public bool isDestructor()
//測試該方法是否為析構(gòu)函數(shù)
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()
//導(dǎo)出該屬性的詳細(xì)信息
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)
//設(shè)置該屬性值
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()
//導(dǎo)出該擴(kuò)展的所有信息
public string getName()
//取得該擴(kuò)展的名字
public string getVersion()
//取得該擴(kuò)展的版本
public ReflectionFunction[] getFunctions()
//取得該擴(kuò)展的所有函數(shù)
public array getConstants()
//取得該擴(kuò)展的所有常量
public array getINIEntries()
//取得與該擴(kuò)展相關(guān)的,在php.ini中的指令信息
public ReflectionClass[] getClasses()
public array getClassNames()
}
?>
關(guān)于如何正確的使用PHP反射問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。
當(dāng)前題目:如何正確的使用PHP反射-創(chuàng)新互聯(lián)
標(biāo)題路徑:http://aaarwkj.com/article28/pijjp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、用戶體驗(yàn)、網(wǎng)站營銷、網(wǎng)站建設(shè)、靜態(tài)網(wǎng)站、ChatGPT
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容