久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

  • <tfoot id='0WaNs'></tfoot>

    <small id='0WaNs'></small><noframes id='0WaNs'>

        <bdo id='0WaNs'></bdo><ul id='0WaNs'></ul>
      <i id='0WaNs'><tr id='0WaNs'><dt id='0WaNs'><q id='0WaNs'><span id='0WaNs'><b id='0WaNs'><form id='0WaNs'><ins id='0WaNs'></ins><ul id='0WaNs'></ul><sub id='0WaNs'></sub></form><legend id='0WaNs'></legend><bdo id='0WaNs'><pre id='0WaNs'><center id='0WaNs'></center></pre></bdo></b><th id='0WaNs'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='0WaNs'><tfoot id='0WaNs'></tfoot><dl id='0WaNs'><fieldset id='0WaNs'></fieldset></dl></div>

      <legend id='0WaNs'><style id='0WaNs'><dir id='0WaNs'><q id='0WaNs'></q></dir></style></legend>
      1. PHP 依賴注入

        PHP Dependency Injection(PHP 依賴注入)
          <tfoot id='GSM2V'></tfoot>
            <bdo id='GSM2V'></bdo><ul id='GSM2V'></ul>
              <tbody id='GSM2V'></tbody>

              1. <small id='GSM2V'></small><noframes id='GSM2V'>

                <i id='GSM2V'><tr id='GSM2V'><dt id='GSM2V'><q id='GSM2V'><span id='GSM2V'><b id='GSM2V'><form id='GSM2V'><ins id='GSM2V'></ins><ul id='GSM2V'></ul><sub id='GSM2V'></sub></form><legend id='GSM2V'></legend><bdo id='GSM2V'><pre id='GSM2V'><center id='GSM2V'></center></pre></bdo></b><th id='GSM2V'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='GSM2V'><tfoot id='GSM2V'></tfoot><dl id='GSM2V'><fieldset id='GSM2V'></fieldset></dl></div>

                <legend id='GSM2V'><style id='GSM2V'><dir id='GSM2V'><q id='GSM2V'></q></dir></style></legend>
                • 本文介紹了PHP 依賴注入的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我正在努力了解依賴注入,并且在很大程度上我理解它.

                  I'm trying to get my head around Dependency Injection and I understand it, for the most part.

                  但是,假設由于某種原因,我的一個類依賴于多個類,而不是在構造函數中將所有這些都傳遞給這個類,是否有更好、更明智的方法?

                  However, say if, for some reason, one of my classes was dependent on several classes, instead of passing all of these to this one class in the constructor, is there a better, more sensible method?

                  我聽說過 DI Containers,這是我解決這個問題的方式嗎?我應該從哪里開始這個解決方案?我是否將依賴項傳遞給我的 DIC,然后將其傳遞給需要這些依賴項的類?

                  I've heard about DI Containers, is this how I would go about solving this problem? Where should I start with this solution? Do I pass the dependencies to my DIC, and then pass this to the class that needs these dependencies?

                  任何能幫我指明正確方向的幫助都會很棒.

                  Any help that would point me in the right direction would be fantastic.

                  推薦答案

                  如果您有多個依賴項需要處理,那么 DI 容器可以成為解決方案.

                  If you have several dependencies to deal with, then yes a DI container can be the solution.

                  DI 容器可以是由您需要的各種依賴對象構成的對象或數組,這些對象會被傳遞給構造函數并解包.

                  The DI container can be an object or array constructed of the various dependent object you need, which gets passed to the constructor and unpacked.

                  假設您需要一個配置對象、一個數據庫連接和一個傳遞給每個類的客戶端信息對象.您可以創建一個包含它們的數組:

                  Suppose you needed a config object, a database connection, and a client info object passed to each of your classes. You can create an array which holds them:

                  // Assume each is created or accessed as a singleton, however needed...
                  // This may be created globally at the top of your script, and passed into each newly
                  // instantiated class
                  $di_container = array(
                    'config' = new Config(),
                    'db' = new DB($user, $pass, $db, $whatever),
                    'client' = new ClientInfo($clientid)
                  );
                  

                  并且您的類構造函數接受 DI 容器作為參數:

                  And your class constructors accept the DI container as a parameter:

                  class SomeClass {
                    private $config;
                    private $db;
                    private $client;
                   
                    public function __construct(&$di_container) {
                      $this->config = $di_container['config'];
                      $this->db = $di_container['db'];
                      $this->client = $di_container['client'];
                    }
                  }
                  

                  代替我上面所做的數組(這很簡單),您還可以將 DI 容器創建為一個類本身,并使用單獨注入其中的組件類對其進行實例化.使用對象而不是數組的一個好處是,默認情況下它將通過引用傳遞給使用它的類,而數組是通過值傳遞的(盡管數組中的對象仍然是引用).

                  Instead of an array as I did above (which is simple), you might also create the DI container as an class itself and instantiate it with the component classes injected into it individually. One benefit to using an object instead of an array is that by default it will be passed by reference into the classes using it, while an array is passed by value (though objects inside the array are still references).

                  對象在某些方面比數組更靈活,盡管最初的編碼更復雜.

                  There are some ways in which an object is more flexible than an array, although more complicated to code initially.

                  容器對象也可以在其構造函數中創建/實例化包含的類(而不是在外部創建它們并將它們傳入).這可以為使用它的每個腳本節省一些編碼,因為您只需要實例化一個對象(它本身實例化其他幾個).

                  The container object may also create/instantiate the contained classes in its constructor as well (rather than creating them outside and passing them in). This can save you some coding on each script that uses it, as you only need to instantiate one object (which itself instantiates several others).

                  Class DIContainer {
                    public $config;
                    public $db;
                    public $client;
                  
                    // The DI container can build its own member objects
                    public function __construct($params....) {
                      $this->config = new Config();
                  
                      // These vars might be passed in the constructor, or could be constants, or something else
                      $this->db = new DB($user, $pass, $db, $whatever);
                  
                      // Same here -  the var may come from the constructor, $_SESSION, or somewhere else
                      $this->client = new ClientInfo($clientid);
                    }
                  }
                  

                  這篇關于PHP 依賴注入的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  Deadlock exception code for PHP, MySQL PDOException?(PHP、MySQL PDOException 的死鎖異常代碼?)
                  PHP PDO MySQL scrollable cursor doesn#39;t work(PHP PDO MySQL 可滾動游標不起作用)
                  PHP PDO ODBC connection(PHP PDO ODBC 連接)
                  Using PDO::FETCH_CLASS with Magic Methods(使用 PDO::FETCH_CLASS 和魔術方法)
                  php pdo get only one value from mysql; value that equals to variable(php pdo 只從 mysql 獲取一個值;等于變量的值)
                  MSSQL PDO could not find driver(MSSQL PDO 找不到驅動程序)

                    <legend id='buqfh'><style id='buqfh'><dir id='buqfh'><q id='buqfh'></q></dir></style></legend>

                        <tbody id='buqfh'></tbody>
                    • <i id='buqfh'><tr id='buqfh'><dt id='buqfh'><q id='buqfh'><span id='buqfh'><b id='buqfh'><form id='buqfh'><ins id='buqfh'></ins><ul id='buqfh'></ul><sub id='buqfh'></sub></form><legend id='buqfh'></legend><bdo id='buqfh'><pre id='buqfh'><center id='buqfh'></center></pre></bdo></b><th id='buqfh'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='buqfh'><tfoot id='buqfh'></tfoot><dl id='buqfh'><fieldset id='buqfh'></fieldset></dl></div>

                      <tfoot id='buqfh'></tfoot>

                        • <bdo id='buqfh'></bdo><ul id='buqfh'></ul>

                            <small id='buqfh'></small><noframes id='buqfh'>

                          • 主站蜘蛛池模板: 在线成人av | 天堂精品视频 | 伊人久久精品一区二区三区 | 美女国产精品 | 成人毛片视频在线播放 | 一区二区三区四区在线 | 国产精品精品 | 天天av网 | 色橹橹欧美在线观看视频高清 | 久久久久一区 | 国产高清视频在线 | 黄色91在线 | 久久久久免费精品国产小说色大师 | 成人一区二区在线 | 日韩精品1区2区3区 成人黄页在线观看 | 韩日一区二区 | 久久国产区 | 中文在线播放 | 一区精品国产欧美在线 | 欧美爱爱视频网站 | 国产激情片在线观看 | 老熟女毛片 | 欧美舔穴 | 亚洲一区二区视频 | 久久精品久久久久久 | 久久久久久免费精品一区二区三区 | 365夜爽爽欧美性午夜免费视频 | 精品一区二区三区中文字幕 | 亚洲一区在线免费观看 | 日韩插插| 中文字幕一区二区三区四区 | 日韩欧美在线观看一区 | 国产精品成人一区二区三区 | 国产精品99999999 | 国产精品久久久久久久久久 | 久久久久久成人 | 国产一区91在线 | 天堂久久久久久久 | 亚洲一区国产精品 | 中文字幕视频网 | 久久国产精品-国产精品 |