<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                [https://learnku.com/articles/32321](https://learnku.com/articles/32321) ``` $http = (new App())->http; ``` 過程: 執行`new App()`實例化時,首先會調用它的構造函數。 ``` public function __construct(string $rootPath = '') { // thinkPath目錄:如,D:\dev\tp6\vendor\topthink\framework\src\ $this->thinkPath = dirname(__DIR__) . DIRECTORY_SEPARATOR; // 項目根目錄,如:D:\dev\tp6\ $this->rootPath = $rootPath ? rtrim($rootPath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR : $this->getDefaultRootPath(); $this->appPath = $this->rootPath . 'app' . DIRECTORY_SEPARATOR; $this->runtimePath = $this->rootPath . 'runtime' . DIRECTORY_SEPARATOR; // 如果存在「綁定類庫到容器」文件 if (is_file($this->appPath . 'provider.php')) { //將文件里的所有映射合并到容器的「$bind」成員變量中 $this->bind(include $this->appPath . 'provider.php'); } //將當前容器實例保存到成員變量「$instance」中,也就是容器自己保存自己的一個實例 static::setInstance($this); // 保存綁定的實例到「$instances」數組中,見對應分析 $this->instance('app', $this); $this->instance('think\Container', $this); } ``` 構造函數實現了項目各種基礎路徑的初始化,并讀取了`provider.php`文件,將其類的綁定并入`$bind`成員變量,`provider.php`文件默認內容如下 ``` return [ 'think\Request' => Request::class, 'think\exception\Handle' => ExceptionHandle::class, ]; ``` 然后將`provider.php`文件你的內容合并到App的 bind(屬性)容器下,$bind 的值是一組類的標識到類的映射 可以接受類、閉包、接口、實現等,不接受類對象(單獨綁定到instances屬性容器) ->http調用一個不存在的http屬性,觸發__get方法(調用get方法在調用make實例化類并返回該類對象,make的第一個參數就是這個不存在的http屬性) ``` public function make(string $abstract, array $vars = [], bool $newInstance = false) { $abstract = $this->getAlias($abstract); //如果已經存在實例,且不強制創建新的實例,直接返回已存在的實例 if (isset($this->instances[$abstract]) && !$newInstance) { return $this->instances[$abstract]; } //如果有綁定,且綁定的是閉包 if (isset($this->bind[$abstract]) && $this->bind[$abstract] instanceof Closure) { //通過反射實執行方法 $object = $this->invokeFunction($this->bind[$abstract], $vars); } else { //通過反射實例化需要的類,比如'think\Http' $object = $this->invokeClass($abstract, $vars); } if (!$newInstance) { $this->instances[$abstract] = $object; } return $object; } ``` 由上可知make主要是執行invokeFunction返回回調函數(閉包)的內容,invokeClass示例化并返回(這兩個函數通過反射機制實現) 值得注意的是 invokeClass 除了返回示例外,還會調用invokeAfter 執行容器回調 ``` public function invokeClass(string $class, array $vars = []) { try { //通過反射實例化類 $reflect = new ReflectionClass($class); } catch (ReflectionException $e) { throw new ClassNotFoundException('class not exists: ' . $class, $class, $e); } if ($reflect->hasMethod('__make')) { //返回的$method包含'__make'的各種信息,如公有/私有 $method = $reflect->getMethod('__make'); //檢查是否是公有方法且是靜態方法 if ($method->isPublic() && $method->isStatic()) { //綁定參數 $args = $this->bindParams($method, $vars); //調用該方法(__make),因為是靜態的,所以第一個參數是null //因此,可得知,一個類中,如果有__make方法,在類實例化之前會首先被調用 return $method->invokeArgs(null, $args); } } //獲取類的構造函數 $constructor = $reflect->getConstructor(); //有構造函數則綁定其參數 $args = $constructor ? $this->bindParams($constructor, $vars) : []; //根據傳入的參數,通過反射,實例化類 $object = $reflect->newInstanceArgs($args); // 執行容器回調 $this->invokeAfter($class, $object); return $object; } ``` 以上代碼可看出,在一個類中,添加`__make()`方法,在類實例化時,會最先被調用 以上最值得一提的是`bindParams()`方法: ``` protected function bindParams(ReflectionFunctionAbstract $reflect, array $vars = []): array { //如果參數個數為0,直接返回 if ($reflect->getNumberOfParameters() == 0) { return []; } // 判斷數組類型 數字數組時按順序綁定參數 reset($vars); $type = key($vars) === 0 ? 1 : 0; //通過反射獲取函數的參數,比如,獲取Http類構造函數的參數,為「App $app」 $params = $reflect->getParameters(); $args = []; foreach ($params as $param) { $name = $param->getName(); $lowerName = self::parseName($name); $class = $param->getClass(); //如果參數是一個類 if ($class) { //將類型提示的參數實例化 $args[] = $this->getObjectParam($class->getName(), $vars); // 如果參數是普通數組 } elseif (1 == $type && !empty($vars)) { $args[] = array_shift($vars); // 如果參數是關聯數組 } elseif (0 == $type && isset($vars[$name])) { $args[] = $vars[$name]; } elseif (0 == $type && isset($vars[$lowerName])) { $args[] = $vars[$lowerName]; // 如果參數有默認值 } elseif ($param->isDefaultValueAvailable()) { $args[] = $param->getDefaultValue(); } else { throw new InvalidArgumentException('method param miss:' . $name); } } return $args; } ``` 這之中,又最值得一提的是`getObjectParam()`方法: ``` protected function getObjectParam(string $className, array &$vars) { $array = $vars; $value = array_shift($array); // 如果傳入的值已經是一個實例,直接返回 if ($value instanceof $className) { $result = $value; array_shift($vars); } else { //實例化傳入的類 $result = $this->make($className); } return $result; } ``` getObjectParam() 方法再一次光榮地調用 make() 方法,實例化一個類,而這個類,正是從 Http 的構造函數提取的參數,而這個參數又恰恰是一個類的實例 ——App 類的實例。到這里,程序不僅通過 PHP 的反射類實例化了 Http 類,而且實例化了 Http 類的依賴 App 類。假如 App 類又依賴 C 類,C 類又依賴 D類…… 不管多少層,整個依賴鏈條依賴的類都可以實現實例化。 總的來說,整個過程大概是這樣的:需要實例化 Http 類 ==> 提取構造函數發現其依賴 App 類 ==> 開始實例化 App 類(如果發現還有依賴,則一直提取下去,直到天荒地老)==> 將實例化好的依賴(App 類的實例)傳入 Http 類來實例化 Http 類。 這個過程,起個裝逼的名字就叫做「依賴注入」,起個摸不著頭腦的名字,就叫做「控制反轉」。 這個過程,如果退回遠古時代,要實例化 Http 類,大概是這樣實現的(假如有很多層依賴): ``` . . . $e = new E(); $d = new D($e); $c = new D($d); $app = new App($c); $http = new Http($app); ``` 這得有多累人。而現代 PHP,交給「容器」就好了。
                  <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>

                              哎呀哎呀视频在线观看