<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自5.0版本以后添加了反射機制,它提供了一套強大的反射API,允許你在PHP運行環境中,訪問和使用類、方法、屬性、參數和注釋等,其功能十分強大,經常用于高擴展的PHP框架,自動加載插件,自動生成文檔,甚至可以用來擴展PHP語言。由于它是PHP內建的oop擴展,為語言本身自帶的特性,所以不需要額外添加擴展或者配置就可以使用 | 類型 | 說明 | | --- | --- | | Reflector | Reflector 是一個接口,被所有可導出的反射類所實現(implement) | | Reflection | 反射(reflection)類 | | ReflectionClass | 報告了一個類的有關信息 | | ReflectionZendExtension | 報告Zend擴展的相關信息 | | ReflectionExtension | 報告了PHP擴展的有關信息 | | ReflectionFunction | 報告了一個函數的有關信息 | | ReflectionFunctionAbstract | ReflectionFunction 的父類 | | ReflectionMethod | 報告了一個方法的有關信息 | | ReflectionObject | 報告了一個對象(object)的相關信息 | | ReflectionParameter | 取回了函數或方法參數的相關信息 | | ReflectionProperty | 報告了類的屬性的相關信息 | ~~~ <?php namespace Extend; use ReflectionClass; use Exception; /** * 用戶相關類 * Class User * @package Extend */ class User{ const ROLE = 'Students'; public $username = ''; private $password = ''; public function __construct($username, $password) { $this->username = $username; $this->password = $password; } /** * 獲取用戶名 * @return string */ public function getUsername() { return $this->username; } /** * 設置用戶名 * @param string $username */ public function setUsername($username) { $this->username = $username; } /** * 獲取密碼 * @return string */ private function getPassword() { return $this->password; } /** * 設置密碼 * @param string $password */ private function setPassowrd($password) { $this->password = $password; } } ~~~ ~~~ $class = new ReflectionClass('Extend\User'); // 將類名User作為參數,即可建立User類的反射類 $properties = $class->getProperties(); // 獲取User類的所有屬性,返回ReflectionProperty的數組 $property = $class->getProperty('password'); // 獲取User類的password屬性ReflectionProperty $methods = $class->getMethods(); // 獲取User類的所有方法,返回ReflectionMethod數組 $method = $class->getMethod('getUsername'); // 獲取User類的getUsername方法的ReflectionMethod $constants = $class->getConstants(); // 獲取所有常量,返回常量定義數組 $constant = $class->getConstant('ROLE'); // 獲取ROLE常量 $namespace = $class->getNamespaceName(); // 獲取類的命名空間 $comment_class = $class->getDocComment(); // 獲取User類的注釋文檔,即定義在類之前的注釋 $comment_method = $class->getMethod('getUsername')->getDocComment(); // 獲取User類中getUsername方法的注釋文檔 ~~~ ~~~ $class = new ReflectionClass('Extend\User'); // 將類名User作為參數,即可建立User類的反射類 $instance = $class->newInstance('youyou', 1, '***'); // 創建User類的實例 $instance->setUsername('youyou_2'); // 調用User類的實例調用setUsername方法設置用戶名 $value = $instance->getUsername(); // 用過User類的實例調用getUsername方法獲取用戶名 echo $value;echo "\n"; // 輸出 youyou_2 $class->getProperty('username')->setValue($instance, 'youyou_3'); // 通過反射類ReflectionProperty設置指定實例的username屬性值 $value = $class->getProperty('username')->getValue($instance); // 通過反射類ReflectionProperty獲取username的屬性值 echo $value;echo "\n"; // 輸出 youyou_3 $class->getMethod('setUsername')->invoke($instance, 'youyou_4'); // 通過反射類ReflectionMethod調用指定實例的方法,并且傳送參數 $value = $class->getMethod('getUsername')->invoke($instance); // 通過反射類ReflectionMethod調用指定實例的方法 echo $value;echo "\n"; // 輸出 youyou_4 try { $property = $class->getProperty('password_1'); $property->setAccessible(true); // 修改 $property 對象的可訪問性 $property->setValue($instance, 'password_2'); // 可以執行 $value = $property->getValue($instance); // 可以執行 echo $value;echo "\n"; // 輸出 password_2 $class->getProperty('password')->setAccessible(true); // 修改臨時ReflectionProperty對象的可訪問性 $class->getProperty('password')->setValue($instance, 'password');// 不能執行,拋出不能訪問異常 $value = $class->getProperty('password')->getValue($instance); // 不能執行,拋出不能訪問異常 $value = $instance->password; // 不能執行,類本身的屬性沒有被修改,仍然是private }catch(Exception $e){echo $e;} ~~~
                  <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>

                              哎呀哎呀视频在线观看