Accessing the DI container from anywhere

Posted by ChrisR on Stack Overflow See other posts from Stack Overflow or by ChrisR
Published on 2012-09-04T07:57:54Z Indexed on 2012/09/04 9:38 UTC
Read the original article Hit count: 269

I've implemented the Symfony2 Dependency Injection container in my Zend Framework project and it works fine in the MVC layer of my application. I've initialized the DIC in my bootstrap and can access it anywhere by calling:

Zend_Controller_Front::getInstance()->getParam('bootstrap')->getDic()

The problem is that there are some parts of my application that do not utilize the Zend Framework application/MVC layer. My CLI tools for example. I could perfectly initialize a new DIC there but that would just be some copy paste work from the Bootstrap file which is asking for trouble down the road (DRY principles, etc)

Is it a better solution to make my DIC available in the Zend_Registry or as a singleton called by a static method DIC::getInstance() for example?

I know Registry and singletons are considered bad things but the DIC is such a high level part of the application that I will probably never run into the problems that make it a bad thing.

Is this a good solution or are there better ways of accomplishing a globally accessible DIC?

© Stack Overflow or respective owner

Related posts about zend-framework

Related posts about dependency-injection