Zend Framework has been right up there as one of the most preferred PHP frameworks for a long time. With massive tech community support that keeps on adding interesting and innovative features to the ZF library, it has emerged as the de-facto PHP Framework for developers globally.
What is DI in ZF2
One such feature set that was implemented into Zend Framework 2 and above, is the Dependency Injection feature, which addresses the problem of managing object dependencies. It denotes a better programming practice of writing more flexible code, that helps edit the dependent class without having to alter class’ code.
In Zend Framework, Dependency Injection facilitates decoupling of the object from the class so that it no longer is applicable to just that class but is adaptable enough to be re-used in other parts of the program as well. DI will reshuffle the object’s responsibilities from within the class to a new external entity. This external entity is nothing but the Controller of the relevant framework.
In Zend, the ‘Zend_Controller_Action’ instance will act as the Controller. However interestingly enough, this instance too is a dependent object, thus largely negating DI principles. Developers could do well to take it a level above by invoking bootstraps. In ZF2, ‘Zend_Application’ is a bootstrapping approach where developers use a method/class to create an object and call it as needed by a controller.
Why DI?