首頁 > php框架 > Laravel > 主體

Laravel Facade 的詳細解讀

藏色散人
發布: 2020-11-09 15:52:46
轉載
2240 人瀏覽過

#下面由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  \Illuminate\Contracts\Foundation\Application  $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(&#39;A facade root has not been set.&#39;);
        }   

        switch (count($args)) {
            case 0:
                return $instance->$method();
            case 1:
                return $instance->$method($args[0]);
            case 2:
                return $instance->$method($args[0], $args[1]);
            case 3:
                return $instance->$method($args[0], $args[1], $args[2]);
            case 4:
                return $instance->$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 facades\Test1Facade;Test1Facade::hello();  // 这是 Facade 调用
登入後複製

解釋:

    ##facades\Test1Facade 呼叫靜態方法hello () 時,由於沒有定義此方法,會呼叫__callStatic;
  1. 在__callStatic 中,首先是取得對應的實例,即
  2. return static::$app->instances[$name ];。這其中的$name,即為facades\Test1 裡的test1
  3. $app, 即為IOC 容器,類別的實例化過程,就交由它來處理。

以上是Laravel Facade 的詳細解讀的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:csdn.net
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!