<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ### :-: **介紹** **PHP中的反射API(Reflection)屬于比較高級的特性,很多朋友沒有深入學習,可能沒有聽說過,這里我們深入研究一下反射的意義。** ***** 反射由一系列可以分析屬性、方法和類的內置類組成。它在某些方面和對象函數相似,比如`get_class_vars()`,但是更加靈活,而且可以提供更多信息。反射API也可與PHP最新的面向對象特性一起工作,如訪問控制、接口和抽象類。舊的類函數則不太容易與這些新特性一起使用。看過框架源碼的朋友應該對PHP的反射機制有一定的了解,像是依賴注入,對象池,類加載,一些設計模式等等,都用到了反射機制。 ***** 通俗的理解就是定義了一個類,通常來講大部分時候只要 new 這個類,得到他的實例,調用他實例的方法就可以運用了,但是如果現在我們想得到這些信息 > 這個類是否有定義構造函數? > 構造函數又需要傳幾個參數? > 這個類有哪些屬性? > 屬性都是否為常量? > 這個類有哪些方法? > 每個方法的訪問修飾符是什么(public/protected/private)? > 每個方法要傳哪些參數? > 每個參數有什么要求,比如有沒強制指定參數類型?參數有沒有有默認值? > 等等。。。 這里我們引用一個圖,這是反射類的繼承圖,每個類的作用都有說明,詳細api查閱手冊 http://www.php.net/manual/zh/book.reflection.php ![](https://box.kancloud.cn/ac4f0e38ff05135204ebc17edc888959_2452x1465.jpg) ### :-: **實例** **構建一個簡單的類** ``` <?php class Student { public $name; protected $age; private $sex; public function __construct($name , $age , $sex) { $this->setName($name); $this->setAge($age); $this->setSex($sex); } public function setName($name) { $this->name = $name; } public function getName() { return $this->name; } protected function setAge($age) { $this->age = $age; } private function setSex($sex) { $this->sex = $sex; } } ``` **建立反射類示例** ``` <?php require_once 'Student.php'; //兩種方法建立一個類的反射 //方法1,實例化后傳入對象到 ReflectionClass $obj = new Student('張三' ,'33 ,' ,0); $class = new ReflectionClass($obj); //方法2 直接傳類名到 ReflectionClass $class = new ReflectionClass('Student'); //實例化 Student 類 //方法1 $instance = $class->newInstanceArgs(array( '張三' , 33 , 0, )); //方法2 $instance = $class->newInstance('張三' ,'33 ,' ,0); //兩種方法等同于 $instance = new Student('張三' ,'33 ,' ,0) //print_r($class); //print_r($instance);exit;; //執行他的方法 $res = $instance->getName(); //var_dump類的所有屬性和方法 //$res = ReflectionClass::export('Person'); //$res = $class::export('Person'); //$res = $class-> __toString (); //返回指定常量 //$res = $class->getConstant('aaa'); //返回所有定義過的常量 //$res = $class->getConstants(); //反射了類的構造函數 //$res = $class->getConstructor (); //獲取指定屬性,包括private的 //$res = $class->getProperty('name'); //$res = $res->getName(); //獲取所有屬性,包括private的 //$res = $class->getProperties(); //獲取文檔注釋 //$res = $class->getDocComment(); // 獲取最后一行的行數 //$res = $class->getEndLine(); // 獲取起始的行號 //$res = $class->getStartLine(); // 獲取類被定義的文件的文件名。 //$res = $class->getFileName (); // 獲取類名 //$res = $class->getName (); // 獲取方法的數組 // ReflectionMethod::IS_PUBLIC 、 ReflectionMethod::IS_PROTECTED 、 ReflectionMethod::IS_PRIVATE 、 ReflectionMethod::IS_ABSTRACT 、 ReflectionMethod::IS_FINAL 的任意組合。 //$res = $class->getMethods (ReflectionMethod::IS_PUBLIC); //獲取getName方法對象 //ReflectionMethod //$res = $class->getMethod('getName'); //執行方法 //$res = $res->invoke($instance); ``` **通過反射類調用方法示例** >[danger] > **thinkphp 5.0 底層在執行我們定義的控制器的方法就是使用的這個方式 參考 App 類里的 invokeMethod 靜態方法** ``` //調用方法示例 $obj = new Student('張三' ,'33 ,' ,0); $class = new ReflectionClass($obj); $instance = $class->newInstance('張三' ,'33 ,' ,0); //建立方法的反射 $method = new ReflectionMethod("Student" , 'setName'); //調用 setName 這個方法 $res = $method->invokeArgs($instance , array( '李四' , )); ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看