Backend and Infrastructure

Dependency Injection Container

This page is archived

We're keeping this page up as a courtesy to folks who may need to refer to old instructions. We don't plan to update this page.

Sprout Video

Note: This is a PHP-based introduction to dependency injection as a concept. It doesn't use a Drupal-specific example. Also, the library it does use, Pimple, is no longer maintained.

Our project now has services, an interface, and is fully using dependency injection. Nice work! One of the downsides of DI is that all the complexity of creating and configuring objects is now your job. This isn’t so bad since it all happens in one place and gives you so much control, but it is something we can improve! If you want to make this easier, the tool you need is called a dependency injection container. A lot of DI containers exist in PHP, but we're going to use Composer to grab the simplest one of all, called Pimple. (If you are unfamiliar with Composer, you should watch The Wonderful World of Composer tutorial.)

Additional resources

The Wonderful World of Composer
Composer download
Pimple