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

  • <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="kykao2q" 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="k2muae0" 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 依賴注入的處理方法,對大家解決問題具有一定的參考價(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?

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

                  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 容器可以是由您需要的各種依賴對象構(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è)您需要一個(gè)配置對象、一個(gè)數(shù)據(jù)庫連接和一個(gè)傳遞給每個(gè)類的客戶端信息對象.您可以創(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ú)注入其中的組件類對其進(jìn)行實(shí)例化.使用對象而不是數(shù)組的一個(gè)好處是,默認(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)建它們并將它們傳入).這可以為使用它的每個(gè)腳本節(jié)省一些編碼,因?yàn)槟恍枰獙?shí)例化一個(gè)對象(它本身實(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 依賴注入的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 可滾動(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="02iwuqw" 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'>

                          • 主站蜘蛛池模板: 济南玻璃安装_济南玻璃门_济南感应门_济南玻璃隔断_济南玻璃门维修_济南镜片安装_济南肯德基门_济南高隔间-济南凯轩鹏宇玻璃有限公司 | 杭州翻译公司_驾照翻译_专业人工翻译-杭州以琳翻译有限公司官网 组织研磨机-高通量组织研磨仪-实验室多样品组织研磨机-东方天净 | 高压无油空压机_无油水润滑空压机_水润滑无油螺杆空压机_无油空压机厂家-科普柯超滤(广东)节能科技有限公司 | 肉嫩度仪-凝胶测试仪-国产质构仪-气味分析仪-上海保圣实业发展有限公司|总部 | 贵州科比特-防雷公司厂家提供贵州防雷工程,防雷检测,防雷接地,防雷设备价格,防雷产品报价服务-贵州防雷检测公司 | 天坛家具官网 | 运动木地板价格,篮球馆体育运动木地板生产厂家_欧氏地板 | 登车桥动力单元-非标液压泵站-非标液压系统-深圳市三好科技有限公司 | 流程管理|流程管理软件|企业流程管理|微宏科技-AlphaFlow_流程管理系统软件服务商 | MOOG伺服阀维修,ATOS比例流量阀维修,伺服阀维修-上海纽顿液压设备有限公司 | 自动售货机_无人售货机_专业的自动售货机运营商_免费投放售货机-广州富宏主官网 | 鑫铭东办公家具一站式定制采购-深圳办公家具厂家直销 | 全自动包衣机-无菌分装隔离器-浙江迦南科技股份有限公司 | 不锈钢电动球阀_气动高压闸阀_旋塞疏水调节阀_全立阀门-来自温州工业阀门巨头企业 | 移动机器人产业联盟官网| 工业机械三维动画制作 环保设备原理三维演示动画 自动化装配产线三维动画制作公司-南京燃动数字 聚合氯化铝_喷雾聚氯化铝_聚合氯化铝铁厂家_郑州亿升化工有限公司 | 高低温万能试验机-复合材料万能试验机-馥勒仪器 | 地磅-地秤-江阴/无锡地磅-江阴天亿计量设备有限公司_ | 上海宿田自动化设备有限公司-双面/平面/单面贴标机 | 新疆系统集成_新疆系统集成公司_系统集成项目-新疆利成科技 | 自动化展_机器人展_机床展_工业互联网展_广东佛山工博会 | 沟盖板_复合沟盖板厂_电力盖板_树脂雨水篦子-淄博拜斯特 | 电杆荷载挠度测试仪-电杆荷载位移-管桩测试仪-北京绿野创能机电设备有限公司 | 橡胶膜片,夹布膜片,橡胶隔膜密封,泵阀设备密封膜片-衡水汉丰橡塑科技公司网站 | VI设计-LOGO设计公司-品牌设计公司-包装设计公司-导视设计-杭州易象设计 | 变频器维修公司_plc维修_伺服驱动器维修_工控机维修 - 夫唯科技 变位机,焊接变位机,焊接变位器,小型变位机,小型焊接变位机-济南上弘机电设备有限公司 | BAUER减速机|ROSSI-MERSEN熔断器-APTECH调压阀-上海爱泽工业设备有限公司 | 热风机_工业热风机生产厂家上海冠顶公司提供专业热风机图片价格实惠 | 植筋胶-粘钢胶-碳纤维布-碳纤维板-环氧砂浆-加固材料生产厂家-上海巧力建筑科技有限公司 | 镀锌方管,无缝方管,伸缩套管,方矩管_山东重鑫致胜金属制品有限公司 | 许昌奥仕达自动化设备有限公司 | 青州搬家公司电话_青州搬家公司哪家好「鸿喜」青州搬家 | 气动|电动调节阀|球阀|蝶阀-自力式调节阀-上海渠工阀门管道工程有限公司 | 上海小程序开发-上海小程序制作公司-上海网站建设-公众号开发运营-软件外包公司-咏熠科技 | 筛分机|振动筛分机|气流筛分机|筛分机厂家-新乡市大汉振动机械有限公司 | 400电话_400电话申请_866元/年_【400电话官方业务办理】-俏号网 3dmax渲染-效果图渲染-影视动画渲染-北京快渲科技有限公司 | 东莞螺杆空压机_永磁变频空压机_节能空压机_空压机工厂批发_深圳螺杆空压机_广州螺杆空压机_东莞空压机_空压机批发_东莞空压机工厂批发_东莞市文颖设备科技有限公司 | 国产频谱分析仪-国产网络分析仪-上海坚融实业有限公司 | 餐饮加盟网_特色餐饮加盟店_餐饮连锁店加盟| 浙江建筑资质代办_二级房建_市政_电力_安许_劳务资质办理公司 | 高精度-恒温冷水机-螺杆式冰水机-蒸发冷冷水机-北京蓝海神骏科技有限公司 |