Laravel Facade 的詳細解讀

下面由laravel教程欄目給大家介紹laravel facade 的詳細解讀,希望對需要的朋友有所幫助!

Laravel Facade 的詳細解讀

大家好,今天帶來的內容是 Laravel 的 Facade 機制實現原理。

Facade的簡單使用

數據庫的使用:

$users = DB::connection('foo')->select(...);

IOC容器

眾所周知,IOC容器是 Laravel 框架的最最重要的部分。它提供了兩個功能,IOC和容器。

  • IOC(Inversion of Control),也叫控制反轉。說白了,就是控制對象的生成,使開發者不用關心對象的如何生成,只需要關心它的使用即可。
  • 而通過IOC機制生成的對象實例需要一個存放的位置,以便之后繼續使用,便是它的容器功能。

這次不準備講解IOC容器的具體實現,之后會有文章詳細解讀它。關于IOC容器,讀者只需要記住兩點即可:

  1. 根據配置生成對象實例;
  2. 保存對象實例,方便隨時取用;

簡化后 Facade 類

<?php namespace facades;  abstract class Facade {     protected Static $app;      /**       * Set the application instance.      *      * @param  IlluminateContractsFoundationApplication  $app      * @return void      */     public static function setFacadeApplication($app)     {            static::$app = $app;     }       /**       * Get the registered name of the component.      *      * @return String      *      * @throws RuntimeException      */     protected static function getFacadeAccessor()     {         throw new RuntimeException(&#39;Facade does not implement getFacadeAccessor method.&#39;);     }      /**       * Get the root object behind the facade.      *      * @return mixed      */     public static function getFacadeRoot()     {            return static::resolveFacadeInstance(static::getFacadeAccessor());     }       /**      * Resolve the facade root instance from the container.      *      * @param  string|object  $name      * @return mixed      */     protected static function resolveFacadeInstance($name)     {         return static::$app->instances[$name]; ????}  ????public?static?function?__callStatic($method,?$args) ????{ ????????$instance?=?static::getFacadeRoot();  ????????if?(!?$instance)?{ ????????????throw?new?RuntimeException('A?facade?root?has?not?been?set.'); ????????}???  ????????switch?(count($args))?{ ????????????case?0: ????????????????return?$instance-&gt;$method(); ????????????case?1: ????????????????return?$instance-&gt;$method($args[0]); ????????????case?2: ????????????????return?$instance-&gt;$method($args[0],?$args[1]); ????????????case?3: ????????????????return?$instance-&gt;$method($args[0],?$args[1],?$args[2]); ????????????case?4: ????????????????return?$instance-&gt;$method($args[0],?$args[1],?$args[2],?$args[3]); ????????????default: ????????????????return?call_user_func_array([$instance,?$method],?$args); ????????}? ????} }

代碼說明:

  1. $app中存放的就是一個IOC容器實例,它是在框架初始化時,通過 setFacadeApplication() 這個靜態方法設置的
  2. 它實現了 __callStatic 魔術方法
  3. getFacadeAccessor() 方法需要子類繼承,返回一個string的標識,通過這個標識,IOC容器便能返回它所綁定類(框架初始化或其它時刻綁定)的對象
  4. 通過 $instance 調用具體的方法

創建自己的Facade:

TEST1 的具體邏輯:

<?php class Test1{ 	public function hello() 	{ 		print("hello world"); 	}}

TEST1 類的Facade:

<?php namespace facades;/**  * Class Test1  * @package facades  *  * @method static setOverRecommendInfo [設置播放完畢時的回調函數]  * @method static setHandlerPlayer [明確指定下一首時的執行類]  */class Test1Facade extends Facade{     protected static function getFacadeAccessor()     {            return &#39;test1&#39;;     }   }

使用:

use?facadesTest1Facade;Test1Facade::hello();??//?這是?Facade?調用

解釋:

  1. facadesTest1Facade 調用靜態方法 hello() 時,由于沒有定義此方法,會調用 __callStatic;
  2. 在 __callStatic 中,首先是獲取對應的實例,即 return static::$app->instances[$name];。這其中的 $name,即為 facadesTest1 里的 test1
  3. $app, 即為 IOC 容器,類的實例化過程,就交由它來處理。

? 版權聲明
THE END
喜歡就支持一下吧
點贊8 分享