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

  • <tfoot id='H9i8f'></tfoot>

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

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

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

          <bdo id='H9i8f'></bdo><ul id='H9i8f'></ul>

        我如何使用“依賴注入"?在簡單的 php 函數中

        How can I use quot;Dependency Injectionquot; in simple php functions, and should I bother?(我如何使用“依賴注入?在簡單的 php 函數中,我應該打擾嗎?)
          <tbody id='E0aR0'></tbody>

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

            • <legend id='E0aR0'><style id='E0aR0'><dir id='E0aR0'><q id='E0aR0'></q></dir></style></legend>

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

                  本文介紹了我如何使用“依賴注入"?在簡單的 php 函數中,我應該打擾嗎?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我一直聽到人們談論依賴注入及其好處,但我并不真正理解.

                  I hear people talking about dependency injection and the benefit of it all the time, but I don't really understand it.

                  我想知道這是否是我一直將數據庫連接作為參數傳遞"問題的解決方案.

                  I'm wondering if it's a solution to the "I pass database connections as arguments all the time" problem.

                  我嘗試閱讀維基百科關于它的條目,但該示例是用 Java 編寫的,所以我不能完全理解它試圖闡明的區別.( http://en.wikipedia.org/wiki/Dependency_injection).

                  I tried reading wikipedia's entry on it, but the example is written in Java so I don't solidly understand the difference it is trying to make clear. ( http://en.wikipedia.org/wiki/Dependency_injection ).

                  我閱讀了這篇關于 php 依賴注入的文章 ( http://www.potstuck.com/2009/01/08/php-dependency-injection/ ),似乎目標不是直接將依賴項傳遞給對象,而是封鎖創建一個對象以及它的依賴項的創建.不過,我不確定如何在使用 php 函數的上下文中應用它.

                  I read this dependency-injection-in-php article ( http://www.potstuck.com/2009/01/08/php-dependency-injection/ ), and it seems like the objective is to not pass dependencies to an object directly, but to cordon off the creation of an object along with the creation of it's dependencies. I'm not sure how to apply that in a using php functions context, though.

                  另外,下面是依賴注入,我是否應該費心嘗試在函數上下文中進行依賴注入?

                  Additionally, is the following Dependency Injection, and should I bother trying to do dependency injection in a functional context?

                  版本 1:(我每天創建但不喜歡的那種代碼)

                  Version 1: (the kind of code that I create, but don't like, every day)

                  function get_data_from_database($database_connection){
                      $data = $database_connection->query('blah');
                      return $data;
                  }
                  

                  版本 2:(不必傳遞數據庫連接,但也許不需要依賴注入?)

                  Version 2: (don't have to pass a database connection, but perhaps not dependency injection?)

                  function get_database_connection(){
                      static $db_connection;
                      if($db_connection){
                          return $db_connection;
                      } else {
                          // create db_connection
                        ...
                      }
                  }
                  
                  function get_data_from_database(){
                     $conn = get_database_connection();
                     $data = $conn->query('blah');
                     return $data;
                  }
                  
                  $data = get_data_from_database();
                  

                  版本3:(對象"/數據的創建是分開的,數據庫代碼是靜止的,所以這可能算作依賴注入?)

                  Version 3: (the creation of the "object"/data is separate, and the database code is still, so perhaps this would count as dependency injection?)

                  function factory_of_data_set(){
                      static $db_connection;
                      $data_set = null;
                      $db_connection = get_database_connection();
                      $data_set = $db_connection->query('blah');
                      return $data_set;
                  }
                  
                  $data = factory_of_data_set();
                  

                  有沒有人有很好的資源或只是洞察力,使方法和好處 - 晶瑩剔透?

                  Anyone have a good resource or just insight that makes the method and benefit -crystal- clear?

                  推薦答案

                  依賴注入是一個大詞,表示我的構造函數中有更多參數".

                  Dependency injection is a big word for "I have some more parameters in my constructor".

                  當你不喜歡全局變量時,這就是你在可怕的 Singleton 浪潮之前所做的:

                  It's what you did before the awfull Singleton wave when you did not like globals :

                  <?php
                  class User {
                      private $_db;
                      function __construct($db) {
                          $this->_db = $db;
                      }
                  }
                  
                  $db   = new Db();
                  $user = new User($db);
                  

                  現在,訣竅是使用單個類來管理您的依賴項,就像這樣:

                  Now, the trick is to use a single class to manage your dependencies, something like that :

                  class DependencyContainer 
                  {
                      private _instances = array();
                      private _params = array();
                  
                      public function __construct($params)
                      {
                          $this->_params = $params;
                      }
                  
                      public function getDb()
                      {
                          if (empty($this->_instances['db']) 
                              || !is_a($this->_instances['db'], 'PDO')
                          ) {
                              $this->_instances['db'] = new PDO(
                                  $this->_params['dsn'],
                                  $this->_params['dbUser'], 
                                  $this->_params['dbPwd']
                              );
                          }
                          return $this->_instances['db'];
                      }
                  }
                  
                  class User
                  {
                      private $_db;
                      public function __construct(DependencyContainer $di)
                      {
                           $this->_db = $di->getDb();
                      }
                  }
                  
                  $dependencies = new DependencyContainer($someParams);
                  $user = new User($dependencies);
                  

                  你一定認為你只是另一個類和更多的復雜性.但是,您的用戶類可能需要像許多其他類一樣記錄消息.只需將 getMessageHandler 函數添加到您的依賴項容器,并將一些 $this->_messages = $di->getMessageHandler() 添加到您的用戶類.其余代碼無需更改.

                  You must think you just another class and more complexity. But, your user class may need something to log messages like lot of other classes. Just add a getMessageHandler function to your dependency container, and some $this->_messages = $di->getMessageHandler() to your user class. Nothing to change in the rest of your code.

                  你會得到很多關于 symfony 的文檔

                  這篇關于我如何使用“依賴注入"?在簡單的 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 找不到驅動程序)
                    <tbody id='fvM7o'></tbody>

                    <tfoot id='fvM7o'></tfoot>

                        • <small id='fvM7o'></small><noframes id='fvM7o'>

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

                            主站蜘蛛池模板: 一级毛片免费 | 免费一区 | 福利视频网站 | 免费一级欧美在线观看视频 | 日韩资源 | a级片在线 | 91精品久久久久久久久久入口 | 国产 欧美 日韩 一区 | 日韩中文字幕一区 | 成人在线免费观看av | 国产精品免费在线 | 欧美日韩在线播放 | 狠狠天天 | 91不卡| 成人夜晚看av | 老牛影视av一区二区在线观看 | 亚洲一区二区三区免费在线 | 亚洲精品日韩一区二区电影 | 一级看片免费视频囗交动图 | 久色| 久久久成 | 日产精品久久久一区二区福利 | 免费视频二区 | 久久国产精品一区二区三区 | 天堂男人av | 欧美精品在线播放 | 日韩a v在线免费观看 | 日日干天天操 | av免费看在线 | 欧美激情久久久 | 免费国产成人av | 亚洲国产精品va在线看黑人 | 精品福利一区二区三区 | 鲁大师一区影视 | 亚洲精品国产成人 | 中文字幕一区二区三区在线观看 | 欧美日韩中文字幕在线 | 日韩欧美大片在线观看 | 亚洲国产成人精 | www日| 成人国产在线观看 |