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

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                [TOC] # 說明 從這一節開始,我們對一次請求(比如web訪問)的生命周期進行分析,涵蓋框架個核心功能。這一節 分析`App`,`Http` 類的實例化過程,同時了解類是如何實現自動實例化的,即依賴注入是怎么實現的。 # 再次從入口文件出發 當訪問一個ThinkPHP搭建的站點,框架最先是從入口文件開始的,然后才是應用初始化、路由解析、控制器調用和響應輸出等操作。 入口文件主要代碼如下: ``` // 引入自動加載器,實現類的自動加載功能(PSR4標準) // 具體參見上一節分析 require __DIR__ . '/../vendor/autoload.php'; // 這一句可分為兩部分分析,App的實例化和調用「http」,具體見下文分析 $http = (new App())->http; $response = $http->run(); $response->send(); $http->end($response); ``` # App實例化 執行 `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, ]; ``` 合并后,`$bind`成員變量的值如下: ![](https://img.kancloud.cn/c9/52/c95263cc87ec4033b9e6f080308ea5b6_365x554.PNG) `$bind`的值是一組類的標識到類的映射。從這個實現也可以看出,**我們不僅可以在`provider.php`文件中添加標識到類的映射,而且可以覆蓋其原有的映射,也就是將某些核心類替換成自己定義的類**。 ## `static::setInstance($this)`實現的作用 如圖所示: ![](https://img.kancloud.cn/93/87/93879f332fc40e75a87d152d15d61bec_285x47.PNG) `think\App`類的`$instance`成員變量指向`think\App`類的一個實例,也就是類自己保存自己的一個實例。 ## `instance()`方法的實現 ``` public function instance(string $abstract, $instance) { $abstract = $this->getAlias($abstract); //保存綁定的實例到「$instances」數組中 //比如,$this->instances["think\App"] = $instance; $this->instances[$abstract] = $instance; return $this; } ``` 其中的`getAlias`方法: ``` public function getAlias(string $abstract): string { //檢查「$bind」中是否保存了名稱到實際類的映射,如 'app'=> 'think\App' //也就是說,只要綁定了這種對應關系,通過傳入名稱,就可以找到實際的類 if (isset($this->bind[$abstract])) { //$abstract = 'app', $bind = "think\App" $bind = $this->bind[$abstract]; //如果「$bind」是字符串,重走上面的流程 if (is_string($bind)) { return $this->getAlias($bind); } } return $abstract; } ``` 執行結果大概是這樣的: ![](https://img.kancloud.cn/4e/93/4e93bdf853a3dcba636c68ee03ce97c5_355x52.PNG) # Http類的實例化以及依賴注入原理 這里,`$http = (new App())->http`,前半部分好理解,后半部分乍一看有點讓人摸不著頭腦,`App`類并不存在`http`成員變量,這里何以大膽調用了一個不存在的東東呢? 原來,`App`類繼承自`Container`類,而`Container`類實現了`__get()` 魔術方法,在PHP中,當訪問到的變量不存在,就會觸發`__get()`魔術方法。該方法的實現如下: ``` public function __get($name) { return $this->get($name); } ``` 實際上是調用`get()`方法: ``` public function get($abstract) { //先檢查是否有綁定實際的類或者是否實例已存在 //比如,$abstract = 'http' if ($this->has($abstract)) { return $this->make($abstract); } // 找不到類則拋出類找不到的錯誤 throw new ClassNotFoundException('class not exists: ' . $abstract, $abstract); } ``` 然而,實際上,主要是`make()`方法: ``` 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()`方法主要靠`invokeClass()`來實現類的實例化(如果綁定的是閉包的話,則靠invokeFunction方法)。該方法具體分析: ``` 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 C($d); $app = new App($c); $http = new Http($app); . . . ``` 這得有多累人。而現代PHP,交給「容器」就好了。 另外,需要提的一點是`make`方法的` $vars`參數,它的形式可以是普通數組、關聯數組,而且數組中元素的值可以是一個類的實例。` $vars`參數的值最終將傳遞給要實例化的類的構造函數或者`__make`方法中對應的參數。
                  <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>

                              哎呀哎呀视频在线观看