加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 我需要一个依赖注入容器吗?

发布时间:2020-12-13 16:17:58 所属栏目:PHP教程 来源:网络整理
导读:我有以下需要在我的程序开始时设置的依赖项: //Set up dependencies$fileSettings = array(12,'extra_lines','close_on_save');$exporter = new Exporter('exporterpath/filename.txt',$fileSettings);$outputStream = new OutputStream();$businessModel =
我有以下需要在我的程序开始时设置的依赖项:

//Set up dependencies
$fileSettings  = array(12,'extra_lines','close_on_save');
$exporter      = new Exporter('exporterpath/filename.txt',$fileSettings);
$outputStream  = new OutputStream();
$businessModel = new BusinessModel('param1','param2');

//Run application
$application   = new Application($exporter,$outputStream,$businessModel);
$application->start();

所以在这里你可以看到我对我的主应用程序有一些依赖关系,这是bootstrap所需的代码,以便注入所需的一切.

这是否需要依赖注入容器?

如果没有,工厂方法会更合适吗?

解决方法

这是一篇很好的依赖注入文章(PHP甚至!).

http://fabien.potencier.org/article/12/do-you-need-a-dependency-injection-container

来自文章:

Most of the time,you don’t need a Dependency Injection Container to
benefit from Dependency Injection.

But when you need to manage a lot of different objects with a lot of
dependencies,a Dependency Injection Container can be really helpful
(think of a framework for instance).

我相信正确的答案与你的场景有多复杂有关.在某些时候,某些地方必须知道如何把所有这些放在一起.如果它变得乏味和丑陋,那么可能是集装箱的时候了.

实际上,你实际上从来没有一个容器.由于它提供的灵活性,它通常只是一个好主意.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读