Zend Framework 2 Core Concepts - Dependency Injection

To master the Zend Framework 2 we need to understand its core concepts. In this series, we work through four of the core ones, starting with Dependency Injection. Join me today to get started learning all about DI.


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

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.


You might also be interested in these tutorials too...

Wed, Jan 2, 2013

Zend Framework 2 Modules - The Application's Heart

Zend Framework 2 Modules - The Application's Heart

If I have seen further it is by standing on the shoulders of giants.

It’s a really exciting time at the moment with Zend Framework 2 gaining so much traction, after being stable for some time now.

Though I and countless others really enjoyed the 1.x series, it did leave some things to be desired - to be fair.

But the more I explore of the 2.x series, the more I honestly can say that I’m very impressed with it. It may not be as fast as the previous series, but with respect to development, there’s so much going for it it’s worth shouting about.

So it really is rewarding and exciting to begin covering all that it has to offer us. In part one of this series, I looked at a central concept of the revised framework - Dependency Injection.

Thu, Aug 23, 2012

Ship-It - Even if it Sucks!

Whether it’s The Cloud, Mobile Development, HTML5, iOS, Android, Windows Phone, Search, Big Data, Queuing or more, we know there’s a wealth of possibility of what we can do. But we then seem to combine that with a misplaced, seemingly schizophrenic, need to be able to do it all and then not ship it until it’s perfect Today we discuss the Ship-It! attitude

Wed, Jan 30, 2013

Zend ServiceManager - Web Application Development Simplified

The Zend ServiceManager simplifies the web application development process by making configuration a breeze. In this, the 4th post introducing Zend Framework 2, you will learn what the ServiceManager is how to use it and how it simplifies applicaiton development time.


Want more tutorials like this?

If so, enter your email address in the field below and click subscribe.

You can unsubscribe at any time by clicking the link in the footer of the emails you'll receive. Here's my privacy policy, if you'd like to know more. I use Mailchimp to send emails. You can learn more about their privacy practices here.

Join the discussion

comments powered by Disqus