久久久久久久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 依賴注入的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

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

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

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

                  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,這是我解決這個(gè)問題的方式嗎?我應(yīng)該從哪里開始這個(gè)解決方案?我是否將依賴項(xiàng)傳遞給我的 DIC,然后將其傳遞給需要這些依賴項(xiàng)的類?

                  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?

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

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

                  推薦答案

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

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

                  DI 容器可以是由您需要的各種依賴對(duì)象構(gòu)成的對(duì)象或數(shù)組,這些對(duì)象會(huì)被傳遞給構(gòu)造函數(shù)并解包.

                  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.

                  假設(shè)您需要一個(gè)配置對(duì)象、一個(gè)數(shù)據(jù)庫連接和一個(gè)傳遞給每個(gè)類的客戶端信息對(duì)象.您可以創(chuàng)建一個(gè)包含它們的數(shù)組:

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

                  并且您的類構(gòu)造函數(shù)接受 DI 容器作為參數(shù):

                  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'];
                    }
                  }
                  

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

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

                  對(duì)象在某些方面比數(shù)組更靈活,盡管最初的編碼更復(fù)雜.

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

                  容器對(duì)象也可以在其構(gòu)造函數(shù)中創(chuàng)建/實(shí)例化包含的類(而不是在外部創(chuàng)建它們并將它們傳入).這可以為使用它的每個(gè)腳本節(jié)省一些編碼,因?yàn)槟恍枰獙?shí)例化一個(gè)對(duì)象(它本身實(shí)例化其他幾個(gè)).

                  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);
                    }
                  }
                  

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

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

                  相關(guān)文檔推薦

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

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

                          • 主站蜘蛛池模板: 欧美一级片黄色 | 精品日韩一区 | 91久色| 99在线国产 | 国产高清视频一区二区 | 四虎影院免费在线 | 精品一区二区电影 | 国产视频不卡一区 | 91精品导航 | 韩日在线观看视频 | 国产精品一区在线观看 | 精品久久久久久亚洲综合网 | 国产午夜精品福利 | 国产精品激情 | 可以看黄的视频 | 国产精品一区二区视频 | 欧美不卡视频 | 久久精品—区二区三区 | 日韩国产欧美一区 | 欧美成人精品一区二区男人看 | 一级毛片,一级毛片 | 成人福利影院 | 欧美日韩中文字幕 | 999热精品 | 一区二区三区在线免费观看 | 免费看日韩视频 | 亚洲电影一区二区三区 | 国产99在线 | 欧美 | 久久久精 | 国产视频中文字幕 | 国产亚洲精品一区二区三区 | 久久久久久久久久久福利观看 | 黄色网页在线 | 99久久婷婷国产综合精品首页 | 国产精品视频久久久久久 | 欧美13videosex性极品 | 波多野结衣二区 | 一区二区三区四区日韩 | 亚洲日本欧美日韩高观看 | 亚州激情 | 能看的av网站 |