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

                  問題描述

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

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

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

                  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,這是我解決這個問題的方式嗎?我應(yīng)該從哪里開始這個解決方案?我是否將依賴項(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?

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

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

                  推薦答案

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

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

                  DI 容器可以是由您需要的各種依賴對象構(gòu)成的對象或數(shù)組,這些對象會被傳遞給構(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è)您需要一個配置對象、一個數(shù)據(jù)庫連接和一個傳遞給每個類的客戶端信息對象.您可以創(chuàng)建一個包含它們的數(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)建為一個類本身,并使用單獨(dú)注入其中的組件類對其進(jìn)行實(shí)例化.使用對象而不是數(shù)組的一個好處是,默認(rèn)情況下它將通過引用傳遞給使用它的類,而數(shù)組是通過值傳遞的(盡管數(shù)組中的對象仍然是引用).

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

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

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

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

                  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 依賴注入的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(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 可滾動游標(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 獲取一個值;等于變量的值)
                  MSSQL PDO could not find driver(MSSQL PDO 找不到驅(qū)動程序)

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

                          • 主站蜘蛛池模板: 日韩在线一区二区三区 | 97香蕉视频| 国产精品一区二区三区免费 | 日韩中文字幕一区 | 久久久一本 | 一区 | 色婷婷在线视频 | 亚洲精品成人在线 | 一区视频 | 免费观看全黄做爰视频 | 国产又粗又猛视频免费 | 久视频在线| 国产欧美在线播放 | 日韩国产精品一区二区 | 亚洲乱码在线观看 | 91久久久久久久久 | 欧美一级在线观看 | 欧美日韩亚洲一区 | 在线观看日韩视频 | 国产成人免费视频 | 国产一区在线看 | 黄色网在线| 91国产丝袜在线播放 | 日韩黄色网址 | 夜夜夜夜操| 青草视频在线播放 | 欧美日韩在线一区二区三区 | 中文字幕精品在线观看 | 毛片一级片| 在线一区视频 | 综合久久久 | www中文字幕 | 亚洲天堂男人天堂 | 日韩免费精品视频 | 三级在线观看视频 | 午夜精品视频 | www.com国产| 国产黄色av | 国产视频成人 | 黄色片视频 | 精品亚洲一区二区三区 |