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

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ## 門面(`Facade`) 門面為容器中的類提供了一個靜態調用接口,相比于傳統的靜態方法調用, 帶來了更好的可測試性和擴展性,你可以為任何的非靜態類庫定義一個`facade`類。 >[info] 系統已經為大部分核心類庫定義了`Facade`,所以你可以通過`Facade`來訪問這些系統類,當然也可以為你的應用類庫添加靜態代理。 下面是一個示例,假如我們定義了一個`app\common\Test`類,里面有一個`hello`動態方法。 ~~~ <?php namespace app\common; class Test { public function hello($name) { return 'hello,' . $name; } } ~~~ 調用hello方法的代碼應該類似于: ~~~ $test = new \app\common\Test; echo $test->hello('thinkphp'); // 輸出 hello,thinkphp ~~~ 接下來,我們給這個類定義一個靜態代理類`app\facade\Test`(這個類名不一定要和`Test`類一致,但通常為了便于管理,建議保持名稱統一)。 ~~~ <?php namespace app\facade; use think\Facade; class Test extends Facade { protected static function getFacadeClass() { return 'app\common\Test'; } } ~~~ 只要這個類庫繼承`think\Facade`,就可以使用靜態方式調用動態類`app\common\Test`的動態方法,例如上面的代碼就可以改成: ~~~ // 無需進行實例化 直接以靜態方法方式調用hello echo \app\facade\Test::hello('thinkphp'); ~~~ 結果也會輸出 `hello,thinkphp`。 >[danger] 說的直白一點,Facade功能可以讓類無需實例化而直接進行靜態方式調用。 如果沒有通過`getFacadeClass`方法顯式指定要靜態代理的類,可以在調用的時候進行動態綁定: ~~~ <?php namespace app\facade; use think\Facade; class Test extends Facade { } ~~~ ~~~ use app\facade\Test; use think\Facade; Facade::bind('app\facade\Test', 'app\common\Test'); echo Test::hello('thinkphp'); ~~~ `bind`方法支持批量綁定,因此你可以在應用的公共函數文件中統一進行綁定操作,例如: ~~~ Facade::bind([ 'app\facade\Test' => 'app\common\Test', 'app\facade\Info' => 'app\common\Info', ]); ~~~ ## 核心`Facade`類庫 系統給內置的常用類庫定義了`Facade`類庫,包括: |(動態)類庫|Facade類| |---|---| | think\App | think\facade\App| | think\Build | think\facade\Build| | think\Cache | think\facade\Cache| | think\Config | think\facade\Config| | think\Cookie | think\facade\Cookie| | think\Debug | think\facade\Debug | | think\Env | think\facade\Env | | think\Hook | think\facade\Hook| | think\Lang | think\facade\Lang| | think\Log | think\facade\Log | | think\Middleware | think\facade\Middleware | | think\Request | think\facade\Request | | think\Response | think\facade\Response| | think\Route | think\facade\Route | | think\Session | think\facade\Session| | think\Url | think\facade\Url | | think\Validate | think\facade\Validate | | think\View | think\facade\View | 所以你無需進行實例化就可以很方便的進行方法調用,例如: ~~~ use think\facade\Cache; Cache::set('name','value'); echo Cache::get('name'); ~~~ >[danger] `think\Db`類的實現本來就類似于`Facade`機制,所以不需要再進行靜態代理就可以使用靜態方法調用(確切的說`Db`類是沒有方法的,都是調用的`Query`類的方法)。 在進行依賴注入的時候,請不要使用`Facade`類作為類型約束,而是建議使用原來的動態類,下面是錯誤的用法: ~~~ <?php namespace app\index\controller; use think\facade\App; class Index { public function index(App $app) { } } ~~~ 應當使用下面的方式: ~~~ <?php namespace app\index\controller; use think\App; class Index { public function index(App $app) { } } ~~~ 為了更加方便的使用系統類庫,系統還給這些常用的核心類庫的`Facade`類注冊了類庫別名,當進行靜態調用的時候可以直接使用簡化的別名進行調用。 |別名類|對應Facade類| |---|---| | App | think\facade\App| | Build | think\facade\Build| | Cache | think\facade\Cache| | Config | think\facade\Config| | Cookie | think\facade\Cookie| | Db | think\Db| | Debug | think\facade\Debug | | Env | think\facade\Env | | Hook | think\facade\Hook| | Lang | think\facade\Lang| | Log | think\facade\Log | | Middleware | think\facade\Middleware | | Request | think\facade\Request | | Response | think\facade\Response| | Route | think\facade\Route | | Session | think\facade\Session| | Url | think\facade\Url | | Validate | think\facade\Validate | | View | think\facade\View | 因此前面的代碼可以改成 ~~~ \Cache::set('name','value'); echo \Cache::get('name'); ~~~ >[danger] Facade類定義了一個實例化的`instance`方法,如果你的類也有定義的話將會失效。
                  <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>

                              哎呀哎呀视频在线观看