As Zend Framework 2 is well and truly here, before some of us who are new to it dive right on in, whether you’re completely new or, like me, migrating from Zend Framework 1, it’s really important to ensure that we understand the core concepts on which it’s based.
Like anything that you want to know well, knowing the core concepts will help you become a master quicker, because you have a more informed understanding of what you’re working with, how it’s composed, the paradigms and methodology it’s built around (and consequently will be most suited to building applications with) and so on.
So this series will be covering those essentials. It’s not going to be going too heavy in to code, more looking at understanding the concepts themselves. Though there will be code, as needed.
The ones that I’ll be looking at in this series are:
In this, the first part in the series, I’m going to go through what dependency injection (DI) is. However, as there are a number of great posts already available on the topic by some very experienced developers, including Pádraic Brady, Ralph Schindler and Zend Framework project lead, Matthew Weier O’Phinney, I’m not going to rehash them.
Instead, below I’ve included 5 of the best quotes available. My aim is so that you can see a series of different perspectives from some really experienced and educated people on what dependency injection is and get abreast of it quickly.
All references contain their respective Urls, which are also included in the further reading section of the post. I encourage you to read the respective posts from which they’re taken, as they provide excellent depth and understanding.
Dependency Injection is like ordering off the menu — but specifying things like, “I’d like to substitute portabella mushrooms for the patties, please.” The waiter then goes and brings your dish, which has portabella mushrooms instead of the hamburger patties listed on the menu.
Source: http://mwop.net/blog/260-Dependency-Injection-An-analogy.html
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.
Source: http://framework.zend.com/manual/2.0/en/modules/zend.di.introduction.html
The basic idea of the Dependency Injection is to have a separate object, an assembler, that populates a field in the lister class with an appropriate implementation for the finder interface, resulting in a dependency diagram along the lines of Figure 2
Source: http://martinfowler.com/articles/injection.html
Dependency injection is a software design pattern that allows a choice of component to be made at run-time rather than compile time. This can be used, for example, as a simple way to load plugins dynamically or to choose mock objects in test environments vs. real objects in production environments.
This software design pattern injects the depended-on element (object or value etc) to the destination automatically by knowing the requirement of the destination.
Source: http://en.wikipedia.org/wiki/Dependency_injection
Dependency Injection is where components are given their dependencies through their constructors, methods, or directly into fields.
Source: http://www.picocontainer.org/injection.html
Dependency injection means giving an object its instance variables. Really. That’s it.
Source: http://www.jamesshore.com/Blog/Dependency-Injection-Demystified.html
So there you have it – 6 great perspectives on dependency injection. I hope that in preparation for using Zend Framework 2 (and further software development generally), this post has helped you better understand one of its key concepts.
What are your thoughts on DI? Share them with me in the comments. I’m keen to know what you think. Stay tuned next week for the next part of the series – Zend\ModuleManager.
If you're new to or migrating to the Zend Framework this book will help you get up and running quickly.
Whether you're completely new to Zend Framework, have experience in PHP or other MVC-based frameworks, this book will teach you what you need to know to successfully develop applications with Zend Framework 2. Register your interest TODAY to get your copy of the book when it's released in April
Why Kohana is an Excellent Alternative to Zend Framework
20 Jul 2012

Writing a simple blog with Zend Framework and mongoDB
07 Nov 2010

Zend Framework 2 – Hydrators, Models and the TableGateway Pattern
15 May 2013

Zend Framework 2 Event Manager – A Gentle Introduction
15 Jan 2013
I like the Symfony version better as it lets me do XML configuration as I come from a Java background (but now doing more PHP). Hopefully Zend will have this soon.
- spam
- offensive
- disagree
- off topic
LikeJeunito thanks for your feedback. I'm not as familiar as I likely should be with Symfony, though I am attempting to learn quickly. Can you give a comparative example, or a link?
- spam
- offensive
- disagree
- off topic
LikeJeunito I had a bit of a read on Pimple during the research for the post and it really attracts me, for it's pure simplicity if nothing else. What are your thoughts on it?
- spam
- offensive
- disagree
- off topic
Likemaltblue If I am not mistaken, you can also configure your container in PHP with Symfony. I think the issue here is whether or not to use PHP to configure code or to use XML configuration. Well that got me thinking and I don't have an opinion at the moment and all I can say I feel more comfortable using XML. This dilemma will be much simpler though if PHP had annotations as a language construct.
- spam
- offensive
- disagree
- off topic
LikeJeunito I'm not aware of the Symfony connection to be honest, but I'm keen to have a look and learn more. I agree with you though, I'm more of an XML fan. To me it just feels right.
But then Yaml and JSON are also good ways to do it - and are lighter than XML. Though proper caching alleviates load to a large degree. XML is really clear and there is loads of documentation for it on the Web and books as well.
- spam
- offensive
- disagree
- off topic
LikeVery nice write-up. Dependency Injection is a must-know for anyone taking a foray into Zend Framework 2. It took me a while to get my head round it but I soon understood that it was a term to describe some things we do as developers without actually giving it a name. It does not help when some writers also make it so abstract that newcomers just step back thinking it is some NASA gizmo. Thanks for sharing!
- spam
- offensive
- disagree
- off topic
LikeStevePopoola thanks for the comments buddy and sorry for not writing sooner. Yeah I sorta felt the same way the more that I read about it. It's good to see it formalised in a way. For me particularly, it's great to see such a variety of ways of doing it in a properly structured way. I'm keen to look at how Symfony does it after reading the comment from Jeunito above and compare the two approaches.
- spam
- offensive
- disagree
- off topic
Like