Introduction to Zend\Di¶
Dependency Injection (here-in called DI) is a concept that has been talked about in numerous places over the web. Simply put, we’ll explain the act of injecting dependencies simply with this below code:
$b = new MovieLister(new MovieFinder());
Above, MovieFinder is a dependency of MovieLister, and MovieFinder was injected into MovieLister. If you are not familiar with the concept of DI, here are a couple of great reads: Matthew Weier O’Phinney’s Analogy, Ralph Schindler’s Learning DI, or Fabien Potencier’s Series on DI.
Zend\Di is an example of an Inversion of Control (IoC) container. IoC containers are widely
used to create object instances that have all dependencies resolved and injected. Dependency
Injection containers are one form of IoC – but not the only form.
Zend Framework 2 ships with another form of IoC as well, ZendServiceManager.
Zend\Di, The ServiceManager is code-driven, meaning that you typically tell it what
class to instantiate, or provide a factory for the given class. This approach offers several
- Easier to debug (error stacks take you into your factories, not the dependency injection container).
- Easier to setup (write code to instantiate objects, instead of configuration).
- Faster (
Zend\Dihas known performance issues due to the approaches used).
Unless you have specific needs for a dependency injection container versus more general Inversion
of Control, we recommend using
Zend\ServiceManager for the above reasons.
Dependency Injection Containers¶
When your code is written in such a way that all your dependencies are injected into consuming objects, you might find that the simple act of wiring an object has gotten more complex. When this becomes the case, and you find that this wiring is creating more boilerplate code, this makes for an excellent opportunity to utilize a Dependency Injection Container.
In it’s simplest form, a Dependency Injection Container (here-in called a DiC for brevity), is an object that is capable of creating objects on request and managing the “wiring”, or the injection of required dependencies, for those requested objects. Since the patterns that developers employ in writing DI capable code vary, DiC’s are generally either in the form of smallish objects that suit a very specific pattern, or larger DiC frameworks.
Zend\Di is a DiC framework. While for the simplest code there is no configuration needed, and the use cases are quite simple; for more complex code, Zend\Di is capable of being configured to wire these complex use cases