photo credit: First Aid. via photopin (license)

Constructor Dependency Injection in Magento 2

Psst… want to see something funny? Go ahead, open up the Product -class in Magento 2 and take a look at it’s constructor:

Scared? Don’t be! What you’re seeing is Magento 2’s Constructor Injection. In this article I’ll show you how to embrace and even love it!

What is Constructor Injection?

One nice thing about Magento 2 is it’s dependency injection mechanism. There are various types of dependency injection in Magento 2, and Constructor Injection is on of them.

Constructor Injection is a design pattern in which you give a class it’s dependencies in it’s constructor. This way, when the class is instantiated, it automatically has the objects it depends on. A simple example:

This example creates a class called Foo , but it requires an instance of Bar  in it’s constructor, because it needs some of Bar ‘s functionality somewhere else. By utilising this pattern you make sure that a developer creates an instance of Bar  before trying to create an instance of Foo .

How does Magento 2 does this?

As with Magento 1, Magento 2 uses the Factory pattern to instantiate it’s classes. With the Factory pattern, you don’t create instances of a class itself, but rather tell ‘the Factory’ (a class that instantiates other classes) what class you want. This way, you can control how and what class is instantiated. In the case of Constructor Injection Magento 2 does the following:

  • It ‘looks’ at the constructor of the class you try to instantiate.
  • Creates instances of the classes that are set as parameters in the constructor.
  • Creates a new instance of the required class with the requested dependencies.
  • Returns the instance.

This is pretty amazing because it gives us great flexibility and ease on how to add dependencies to our classes.

An example

Please note that we use the boilerplate module from the article ‘Creating a module in Magento 2‘ as the entry point for this example.

Let’s say we have a helper class that needs the formatUrlKey() -method of Magento\Catalog\Model\Product\Url to generate a ‘slug’ of a string. We can simply do this with Constructor Injection:

Pretty neat huh? Note that we don’t have to take care of instantiating our Url  model, Magento 2’s Constructor Injection takes care of that for us. There are some caveats though. As stated in the documentation, not all classes are injectable. In general, classes that are injectable are those that can be instantiated the need of any external input (like ID’s or credentials).

In conclusion

Dependency Injection (with Constructor Injection in general) is one of several powerful new features that Magento 2 has to offer. I had to say, when I saw the constructors of some classes for the first time I did not know what to think. They just seemed so … big! But now with the knowledge of Constructor Injection and having used it more than several times in my own modules I have to say that I’m pretty enthusiastic about this new mechanism introduced in Magento 2.

This post is part of the series Magento 2 Development from Scratch.

Visitors give this article an average rating of 4.5 out of 5.

How would you rate this article?

Leave a Reply