Get a Free Consultation

    Search for:

    What is Magento 2 Dependency Injection?

    Last Updated | July 6, 2023

    What is Dependency Injection?

    In order to understand Magento 2 dependency injection, we must first have a clear understanding of classes. A class could be considered as a blueprint of an object. It doesn’t exists itself, but has all the data needed to create such an object. A class states all the attributes that make up an object. For example, if a human is an object, the class will contain all the attributes exhibited by the human such as age, height, weight, and other data.

    Now to understand dependency injections, if simply put, dependency is a relationship that is required by a class from another external source or class that the previous class requires to function correctly. The injection is a method that can pass that particular dependency relationship to the dependent class. For example, if I have a class X that fetches data using class Y, we can say that my class X is dependent on class Y.

    Mind you that it is not the vice versa. One of the most iconic example to elaborate dependency injections was explained by John Munsch, who gave the idea of dependency by using a refrigerator and a kid. When the kid wants to get something from the refrigerator, they can get it themselves.

    However, they may cause problems such as leaving the door open or looking for something that is no longer in the fridge. What the kid must do is to ask their mom or dad for what he/she needs. This means that the kid, who is a collaborating class, should rely on the other foundation classes, the parents, to achieve the desired results. This dependency injection in Magento is a win-win situation for users.

    This is because the object is passed to the dependent class, instead of allowing the other dependent class to find the object or create one from scratch. For people who have no experience In PHP frameworks, it is quite difficult to elaborate it for them at such an advanced level so that they can make the dependency injection in Magento by themselves. In order to create such associations for novice users, it is best to hire Magento developers from a certified Magento web development company. They will be able to create and physically show you the changes in the live code as well.

    Magento 2 Dependency Injection

    What are the basics of Magento 2 Dependency Injection?

    There are several basics of dependency injections in Magento 2 that need to be understood. If I have a class in Magento 2 that makes use of a Magento 2 helper class, can correctly state that my original class has a dependency on that Magento 2 helper object. An example for helper classes in real life can be explained using a scenario. If person A is constructing a house, they will begin with the design of the walls and the pillars.

    But when it comes to the entry point of the house, you realize that you need a gate. So would you make it yourself or directly purchase from a vendor. Of course the latter is a more pragmatic approach. This is how a dependency class makes use of helper classes in Magento 2. This can decouple the original class from the construction of its dependent class. This is basically how the helper class is utilized in Magento 2.  

    Magento 2 utilizes the dependency injection to replace its original functionality that was first provided by the Mage class in Magento 1. x.

    The dependency injection in Magento can also be used as a design pattern that can directly allow dependencies to be declared to external objects that then supply those dependencies. Generally, interfaces are used to declare dependencies, and then the dependencies are directed to another class that will provide the concrete implementation for the interfaces. This can be used for loose coupling of code because object A will no longer be responsible for generating its own dependencies.

    It is object B that will decide the implementations on run time. This will in turn provide implementations to object A based on the desired behavior or different configurations for use as per scenario. This is an extremely important building block for basic because it helps extension developers in their work. This is because the dependency injection in Magento formulates the basis of how Magento produces its own classes.

    Dependency inversion principle.

    Another basic but important principle for dependency injection in Magento is to be aware of the dependency inversion principle. This principle makes use of abstractions/ interfaces/ virtual classes in your code to reduce the direct dependency in code. As a result, the high-level classes make use of the abstractions of the low-level classes instead of collaborating with them directly.

    When abstract classes/ interfaces are used in the Magento code, it results in a decreased risk of incompatibility bugs when Magento changes the fundamental application framework for the interfaces. This ensures that the class can function accurately instead of worrying about how the class implements the functions. The Magento codebase follows the inversion principle. This results in users having mapping their own customized implementations of Magento abstract classes to other services using a dependent class or a di.xml file.

    Compiling the dependencies.

    Magento uses the code compiler tool that can collect all of the dependency information of classes and then store them in subsequent files. During the process of class generation, the object manager uses the information to instantiate concrete objects in the code. However, there are some service classes that do not exist in the codebase such as interceptors, proxies, and factories that can be declared in the configuration of code. These service classes can then be generated with the help of the compiler.

    The Magento 2 platform makes use of the automatic dependency injection (ADI) which is then further derived to the constructor injections. In Magento 2, this means the passing of objects in the parameter of a constructor will aid the developer in following the rules for injection. The alternate to the Mage class method of Magento 1 is the dependency injection in Magento 2.

    Dependency types.

    Magento dependencies have two major types as well. One is the injectable dependency and the other is the non-injectable dependency.


    The newable type consists of objects for which injections are not possible. They can only be instantiated by creating a new class every time they are required. The objects that require external user inputs or data from databases, fall into this category of transient objects. Even if you try to inject these objects in the back0end code, you will receive an incorrect or incomplete object due to an error that will not let the object be instantiated.

    For example, one cannot depend on a model object such as a product. This is because a product ID or an explicit request an empty or new instance will be needed. But since we will not be able to specify the data inside the signature of the constructor, Magento will not inject the object. In order to get around this limitation, only the injectable objects can depend on the library routines like factories that can produce non-injectable objects.


    These injectable objects are obtained through dependency injection. They themselves are a singleton service object. The object manager uses the store’s configurations of the d.xml file to instantiate the objects and then inject them into constructor calls. The injectable objects can also depend on other injectable objects in the constructors. However, this is only possible when the dependency chain never circles back to the original object that is also injectable.

    Magento 2 Dependency Injection