Zend Framework 2 Core Concepts – Dependency Injection

Zend Framework 2

Synopsis

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:

  • Dependency Injection
  • Modules (Module Manager)
  • Events (Event Manager)
  • Services (Service Manager)

Dependency Injection

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.

Matthew Weier O’Phinney

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

Zend Framework 2 Manual

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

Martin Fowler

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

Dependency Injection from Martin Fowler

Dependency Injection from Martin Fowler

Source: http://martinfowler.com/articles/injection.html

Wikipedia

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

Pico Container Website (via Fabian Potencier)

Dependency Injection is where components are given their dependencies through their constructors, methods, or directly into fields.

Source: http://www.picocontainer.org/injection.html

James Shore

Dependency injection means giving an object its instance variables. Really. That’s it.

Source: http://www.jamesshore.com/Blog/Dependency-Injection-Demystified.html

Summing Up

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.

Further Reading

  • http://blog.astrumfutura.com/2011/10/zend-framework-2-0-dependency-injection-part-1/
  • http://ralphschindler.com/2011/05/18/learning-about-dependency-injection-and-php
  • http://mwop.net/blog/260-Dependency-Injection-An-analogy.html
  • http://framework.zend.com/manual/2.0/en/modules/zend.di.introduction.html
  • http://martinfowler.com/articles/injection.html](http://martinfowler.com/articles/injection.html
  • http://en.wikipedia.org/wiki/Dependency_injection
  • http://www.picocontainer.org/injection.html](http://www.picocontainer.org/injection.html
  • http://fabien.potencier.org/article/11/what-is-dependency-injection
  • http://www.jamesshore.com/Blog/Dependency-Injection-Demystified.html

Your Thoughts?

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.

Articles featured
  • StevePopoola

    Very 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!

    • http://www.matthewsetter.com/ maltblue

      @StevePopoola 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.

  • StevePopoola

    Very 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!

  • Jeunito

    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.

    • http://www.matthewsetter.com/ maltblue

      @Jeunito 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?

    • http://www.matthewsetter.com/ maltblue

      @Jeunito 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?

      • Jeunito

        @maltblue  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.

        • http://www.matthewsetter.com/ maltblue

          @Jeunito 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.