The PHP podcast where everyone chimes in.

Originally aired on

April 14th, 2017

061: Dependency Injection

Dependency Injection has been a design principle that the PHP community has embraced more fully than a lot of other programming communities. There's even an official PHP-FIG standard being discussed called PSR-11 which will standardize dependency injection containers. Today we chat all things dependency injection.

with


Dependency Injection & PSR-11 Show Summary


What does dependency injection give us?

  • Flexibility
  • Avoids tight coupling
  • Allows component re-use
  • Provides separation of concerns
  • Makes objects more testable
  • Provides inversion of control

How do we implement dependency injection?

  • Pass in objects we need as parameters
  • Avoid the new keyword inside our object code
  • Push object instantiation outside of our objects (this is where DI containers can help us)

When should we use dependency injection?

  • Services or Repositories which we want to exchange within our code base
  • Anywhere we want to be able to use more than one implementation
  • Anywhere we want to be able to swap out an object under testing
  • Sometimes we can directly instantiate objects, for example domain objects and helper classes

What role do interfaces play in dependency injection?

  • Interfaces help us to decouple dependencies from their implementation
  • We should be able to exchange a dependency with another that conforms to the same interface
  • There are some things an interface doesn't capture (e.g: the exceptions that can be thrown by a method)

Which tools can we use to help with dependency injection?

PHP dependency injection containers fall into one of 3 categories:

PSR-11

  • PSR-11 addresses one problem: How do we fetch an object from a container?
  • 2 methods: get() and has()
  • Many components (e.g: frameworks or routers) are currently tightly coupled to a particular container implementation
  • Standardizing container implementation will help to make components more generic

How did dependency injection become so popular in PHP?

  • As unit testing has become more important, so has DI. The two compliment each other
  • The first versions of frameworks like Zend and Symfony were monolithic. In version 2 these frameworks started to split into smaller components. Using dependency injection helped them to decouple.
  • Early implementations borrowed/ported the concept from Spring in Java

Stephan Hochdörfer


David Stockton


David Négrier

  • Mouf, the only framework that does graphical dependency injection (drag'n'drop all the things!)
  • container-interop/service-provider, an attempt to have a framework-agnostic way to put entries in a container


Developer Shout-Out

The Developer Shout-Out recognizes developers in the community for their contributions.

For this episode the panel guests, Stephan, David, David, and Matthias nominated Matthieu Napoli for the Developer Shout-Out segment.

Thank you, Matthieu Napoli for your work on PHP-DI & all your other contributions to the community. A $50 Amazon gift card is on its way to you.

$50 Amazon gift card sponsored by Zend Training

Zend Training

Professional Training for Professional PHP developers

Show Notes Credit

Chris Shaw

Thank you Chris Shaw for authoring the show notes for this episode!

If you'd like to contribute show notes and totally get credit for it, check out the show-notes repo!