Package Home

Zend Framework 2 Documentation (Manual)

PHK Home

File: /modules/zend.di.introduction.html

Size:12275
Storage flags:no_autoload,compress/gzip (33%)

Introduction to Zend\Di — Zend Framework 2 2.4.2 documentation

Introduction to Zend\Di

Dependency Injection

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:

1
$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.

Note

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. Unlike 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 benefits:

  • 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\Di has 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

Table Of Contents

Previous topic

Dumping Variables

Next topic

Zend\Di Quickstart

This Page

Note: You need to stay logged into your GitHub account to contribute to the documentation.

Edit this document

Edit this document

The source code of this file is hosted on GitHub. Everyone can update and fix errors in this document with few clicks - no downloads needed.

  1. Login with your GitHub account.
  2. Go to Introduction to Zend\Di on GitHub.
  3. Edit file contents using GitHub's text editor in your web browser
  4. Fill in the Commit message text box at the end of the page telling why you did the changes. Press Propose file change button next to it when done.
  5. On Send a pull request page you don't need to fill in text anymore. Just press Send pull request button.
  6. Your changes are now queued for review under project's Pull requests tab on GitHub.

For more information about the PHK package format: http://phk.tekwire.net