pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

  • <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="d7lntvx" 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="bpv7vnf" 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="vzljp5b" id='fvM7o'><tfoot id='fvM7o'></tfoot><dl id='fvM7o'><fieldset id='fvM7o'></fieldset></dl></div>
                          • <bdo id='fvM7o'></bdo><ul id='fvM7o'></ul>

                            主站蜘蛛池模板: 精密五金冲压件_深圳五金冲压厂_钣金加工厂_五金模具加工-诚瑞丰科技股份有限公司 | 复合土工膜厂家|hdpe防渗土工膜|复合防渗土工布|玻璃纤维|双向塑料土工格栅-安徽路建新材料有限公司 | 华东师范大学在职研究生招生网_在职研究生招生联展网 | POM塑料_PBT材料「进口」聚甲醛POM杜邦原料、加纤PBT塑料报价格找利隆塑料 | 闭端端子|弹簧螺式接线头|防水接线头|插线式接线头|端子台|电源线扣+护线套|印刷电路板型端子台|金笔电子代理商-上海拓胜电气有限公司 | 818手游网_提供当下热门APP手游_最新手机游戏下载 | 影合社-影视人的内容合作平台 | 机制砂选粉机_砂石选粉机厂家-盐城市助成粉磨科技有限公司 | 早报网| 深圳展厅设计_企业展馆设计_展厅设计公司_数字展厅设计_深圳百艺堂 | 除尘布袋_液体过滤袋_针刺毡滤料-杭州辉龙过滤技术有限公司 | 海德莱电力(HYDELEY)-无功补偿元器件生产厂家-二十年专业从事电力电容器 | 压缩空气冷冻式干燥机_吸附式干燥机_吸干机_沪盛冷干机 | PC阳光板-PC耐力板-阳光板雨棚-耐力板雨棚,厂家定制[优尼科板材] | 西安微信朋友圈广告投放_微信朋友圈推广_西安度娘网络科技有限公司 | Copeland/谷轮压缩机,谷轮半封闭压缩机,谷轮涡旋压缩机,型号规格,技术参数,尺寸图片,价格经销商 CTP磁天平|小电容测量仪|阴阳极极化_双液系沸点测定仪|dsj电渗实验装置-南京桑力电子设备厂 | 拖链电缆_柔性电缆_伺服电缆_坦克链电缆-深圳市顺电工业电缆有限公司 | 上海小程序开发-小程序制作-上海小程序定制开发公司-微信商城小程序-上海咏熠 | 十字轴_十字轴万向节_十字轴总成-南京万传机械有限公司 | 间甲酚,间甲酚厂家-山东祥东新材料 | 打造全球沸石生态圈 - 国投盛世 锂电混合机-新能源混合机-正极材料混料机-高镍,三元材料混料机-负极,包覆混合机-贝尔专业混合混料搅拌机械系统设备厂家 | 美国查特CHART MVE液氮罐_查特杜瓦瓶_制造全球品质液氮罐 | 众能联合-提供高空车_升降机_吊车_挖机等一站工程设备租赁 | 大_小鼠elisa试剂盒-植物_人Elisa试剂盒-PCR荧光定量试剂盒-上海一研生物科技有限公司 | 危废处理系统,水泥厂DCS集散控制系统,石灰窑设备自动化控制系统-淄博正展工控设备 | 上海新光明泵业制造有限公司-电动隔膜泵,气动隔膜泵,卧式|立式离心泵厂家 | 并网柜,汇流箱,电控设备,中高低压开关柜,电气电力成套设备,PLC控制设备订制厂家,江苏昌伟业新能源科技有限公司 | 除甲醛公司-甲醛检测-广西雅居环境科技有限公司 | 新能源汽车教学设备厂家报价[汽车教学设备运营18年]-恒信教具 | 智能风向风速仪,风速告警仪,数字温湿仪,综合气象仪(气象五要素)-上海风云气象仪器有限公司 | 深圳希玛林顺潮眼科医院(官网)│深圳眼科医院│医保定点│香港希玛林顺潮眼科中心连锁品牌 | 香蕉筛|直线|等厚|弧形|振动筛|香蕉筛厂家-洛阳隆中重工 | 【MBA备考网】-2024年工商管理硕士MBA院校/报考条件/培训/考试科目/提前面试/考试/学费-MBA备考网 | 钢衬四氟管道_钢衬四氟直管_聚四氟乙烯衬里管件_聚四氟乙烯衬里管道-沧州汇霖管道科技有限公司 | 电解抛光加工_不锈钢电解抛光_常州安谱金属制品有限公司 | 电气控制系统集成商-PLC控制柜变频控制柜-非标自动化定制-电气控制柜成套-NIDEC CT变频器-威肯自动化控制 | 杭州标识标牌|文化墙|展厅|导视|户内外广告|发光字|灯箱|铭阳制作公司 - 杭州标识标牌|文化墙|展厅|导视|户内外广告|发光字|灯箱|铭阳制作公司 | 步入式高低温测试箱|海向仪器| RTO换向阀_VOC高温阀门_加热炉切断阀_双偏心软密封蝶阀_煤气蝶阀_提升阀-湖北霍科德阀门有限公司 | 碳化硅,氮化硅,冰晶石,绢云母,氟化铝,白刚玉,棕刚玉,石墨,铝粉,铁粉,金属硅粉,金属铝粉,氧化铝粉,硅微粉,蓝晶石,红柱石,莫来石,粉煤灰,三聚磷酸钠,六偏磷酸钠,硫酸镁-皓泉新材料 | CCE素质教育博览会 | CCE素博会 | 教育展 | 美育展 | 科教展 | 素质教育展 |