In this article, our developer Jochem explains the concept of dependency injection, how to use it, and the impact of use on Drupal 8.
First Off: What is Dependency Injection?
“Dependency injection is a technique whereby one object (or static method) supplies the dependencies of another object." - Wikipedia
It's when components are provided with dependencies through their constructors, methods, or directly in fields. These are only some of the definitions of dependency injection, but what does this mean in practice? Rather than building or finding an object/service in your class, it’s handed to you as a dependency. For example, it's injected into your class through your constructor.
How to use Dependency injection
Let’s try this by using an example. Let’s say we have a class called 'computer' in a module called 'computer'.
Instantiating this class would go like this:
Rather basic so far, but now let’s assume this computer needs to work with a different hard drive. We would need to extend the class and overwrite the constructor.
However, we would inject the hard drive into the computer like so:
And instantiating would now go like this:
So, why is this so much better? Let’s say we want to create another computer with a different hard drive. This would be very easy to do, just like this:
In a nutshell, this is what dependency injection is. Rather than letting the computer class define the hard drive itself, we hand it to the computer in its constructor. This provides a developer with the freedom to create a computer object with different components without having to extend classes and override constructors. Sometimes this is also referred to as inversion of control.
The benefits of dependency injection
So now we know what dependency injection is and how we can use it. But what are the advantages of dependency injection? Let’s take a quick look at Drupal 8, which provides a lot of services out of the box. A service for sending e-mails, a service for generating UUIDs, and many more.
A service is nothing more than a PHP Object that can perform a certain (global) task. These services can be injected into other services without being dependent on each other.Their purpose is isolated and can easily be swapped out by other components later. This way your code will be cleaner and decoupled. In my Computer example, it means that injecting a different hard drive or making changes to the original hard drive should not make the computer crash.
2 Reusable code
Another advantage of dependency injection is that your code will be more reusable since there are no direct dependencies on other entities (objects or classes) in your code.
3 Testable code
Your code becomes easier to test when you use dependency injection. Dependencies can be injected and therefore, be replaced by mock implementations that can be configured.
Drupal 8 and dependency injection
What does this mean for Drupal 8? In Drupal 8, you can create your own services. Services are small objects that can perform global tasks. Drupal 8 also has a lot of services available out of the box. Here you can see some of those. These services aren't available in your own objects unless you inject them. So basically in Drupal 8, we don't have everything that Drupal 8 offers out of the box available, but we can just choose what to load in every circumstance. This reduces the load drastically and improves performance.
Did you enjoy this explanation? What else would you like to learn about? Let us know in the comments!
- Dependency injection and the art of services and containers
- Drupal 8: Properly Injecting Dependencies Using DI
- DrupalCon New Orleans 2016: Dependency injection in Drupal 8