本文實例講述了php面向對象之反射功能與用法。分享給大家供大家參考,具體如下:
個人對反射定義的理解:
首先得說說什么叫反射。對于一個新手來說,反射這個概念常常給人一種似懂非懂的 感覺,不知道該如何下手操作。
反射是指:指在PHP運行狀態中,擴展分析PHP程序,導出或提取出關于類、方法、屬性、參數等的詳細信息,同時也包括注釋。這種動態獲取的信息以及動態調用對象的方法 的功能稱為反射API。反射是操縱面向對象范型中元模型的API,其功能十分強大,可幫助我們構建復雜,可擴展的應用。(注意:php中這種反向操作,實在PHP5之后才完全具備)
下面在此我用實例進行說明:
class test{     private   $A;     public   $B;     protected  $C;     public function test(){       return "this is a test function";     }}//實例化一個反射類ReflectionClass$obj=new ReflectionClass('test');echo $obj."<br>";//實例化test類,并訪問其test方法$obj2=$obj->newInstance();echo $obj2->test();個人實例返回結果:
/** * xxx.php * ============================================== * Copy right 2012-2015 * ---------------------------------------------- * This is not a free software, without any authorization is not allowed to use and spread. * ============================================== * @Author:YeXianMing * @Email:LangWaiShiGe@hotmail.com * @Version:zend studio10.6.2 php5.4.38 apache2.2 */ Class [ class test ] { @@ D:/www/MyProjecttest/index5.php 13-21 - Constants [0] { } - Static properties [0] { } - Static methods [0] { } - Properties [3] { Property [ private $A ] Property [ public $B ] Property [ protected $C ] } - Methods [1] { Method [ public method test ] { @@ D:/www/MyProjecttest/index5.php 18 - 20 } } }this is a test function希望本文所述對大家PHP程序設計有所幫助。
新聞熱點
疑難解答
圖片精選