我需要在程序启动时设置以下依赖项:
//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 所需的代码,以便注入(inject)所需的一切。
这是否保证依赖注入(inject)容器?
如果不是,工厂方法会更合适吗?
最佳答案
这是一篇很好的依赖注入(inject)文章(甚至是 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).
我认为正确答案与您的场景的复杂程度有关。在某个时候,某个地方的某些东西必须知道如何将所有这些放在一起。如果它变得乏味和丑陋,那么可能是时候使用容器了。
实际上,您实际上从未必须拥有一个容器。这通常只是一个好主意,因为它提供了灵 active 。
关于php - 我这里需要依赖注入(inject)容器吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12129183/