PHP反射API_PHP教程

來源:互聯網
上載者:User
反射的理解
它是指在php的運行狀態中,擴充分析php程式,匯出或者提取出關於類、方法、屬性、參數等詳細資料,甚至包括注釋。這種動態擷取的資訊以及動態調用對象的方法的功能稱為反射API。反射是操縱物件導向模型中元模型的API,其功能十分強大,可以協助我們構建複雜,可擴充的應用。(ps:包括在原廠模式中的使用)
反射API是php內建的oop技術擴充,包括一些類、異常和介面,綜合使用他們可用來協助我們分析其它類,介面,方法,屬性和擴充。這些oop擴充被稱為反射。
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()
}
?>
原廠模式應用:
[php]
class MoveDataFactory
{
/**
* Description:簡單原廠模式,根據mode選取不同執行個體化對象
* @return 對象執行個體
*/
public function GetMoveClass($classname)
{
$reflectionclass = new ReflectionClass($classname);
return $reflectionclass->newInstance();
}
}

http://www.bkjia.com/PHPjc/477934.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/477934.htmlTechArticle反射的理解 它是指在php的運行狀態中,擴充分析php程式,匯出或者提取出關於類、方法、屬性、參數等詳細資料,甚至包括注釋。這種動態...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.