The Basics of Dependency Injection in Magento 2

The Most Popular Extension Builder for Magento 2

With a big catalog of 234+ extensions for your online store

Dependency Injection is a unique design pattern that implements control inversion and provides the ability to follow the principle of dependency inversion. It is a technique that enables loose coupling. A number of latest software application frameworks support Dependency Injection including TypeScript, Spring, Google Guice, Microsoft Managed Extensibility Framework (MEF), etc.

In Magento 2, one of the most significant changes is the use of Dependency Injection. With this design pattern, the codebase of Magento 2 has changed a lot, and many new things have been introduced. And now it has the same functionality as provided by Mage class in Magento 1.

In this article, I will explain the basics of Dependency Injection in Magento 2 to help those who are new to this topic.

What is Dependency Injection?

Dependency is the object which is required by the class from an external source and injection is the method of passing that particular dependency to the dependent class. For example, if you have a class that fetches some data by using Magento 2 Observer Class, we can say that your class has a dependency on that Magento 2 Observer Object.

Still confused? Take a look at this fantastic example by John Munsch, who has explained dependency injection to a 5-year-old:

“When you go and get things out of the refrigerator for yourself, you can cause problems. You might leave the door open or get something Mommy or Daddy doesn’t want you to have. You might even be looking for something we don’t even have or which has expired. What you should be doing is stating a need, “I need something to drink with lunch,” and then we will make sure you have something when you sit down to eat.”

It means that the collaborating classes (5-year-old) should rely on the foundation classes (parents) to provide the desired results. Dependency Injection is a win-win situation for everyone, it passes the object to the dependent class, rather than allowing the dependent class to build or find the object from scratch.

Dependency Injection in Magento 2

Dependency Injection in Magento 2 provides a high-value concept of loose coupling modules together. If you want to inject dependency into a particular class, you can simply do it by using two types of Dependency Injections in Magento 2.

Constructor Injection

Constructor Injection is one of the commonly used ways to inject dependencies in Magento 2. In this type of DI, all you need to do is add a parameter in the class constructor to inject the dependency. It should be the first choice because Constructor Injection addresses the most simple scenario where a class requires one or more dependencies.

For example, I have a Helper class that can be found in Namespace/ModuleName/Helper/Data.php file with the following code:

<?php
namespace Namespace\ModuleName\Helper;
use \Magento\Framework\App\Helper\AbstractHelper;
class Data extends AbstractHelper
{
       public function HelperDemo()
       {
               //Do Something Here
       }
}

In the above Helper Class, I have created a function HelperDemo() that can be called anywhere in Magento 2 using the Constructor Injection.

<?php
class DependentClass
{
       public function __construct(Namespace\ModuleName\Helper\Data $helper)
       {
               $this->helper = $helper;
       }
       public function MyFunction()
       {               $this->helper->HelperDemo();
       }
}

In the above code sample, the DependentClass declares its dependency on the Helper class in its constructor. This way I can call the HelperDemo() function anywhere within the class.

Method Injection

Method injection is another type of ID used in Magento 2, which passes the dependency as a method parameter to use it in the class. Method injection can be used best when the dependency may vary on each method call. When an object requires performing specific actions on a dependency that cannot be injected, it is recommended to use the Method Injection.

<?php
namespace Namespace\ModuleName\Observer\Product;
use Magento\Framework\Event\ObserverInterface;
class Data implements ObserverInterface
{
    /**
     * @param Magento\Framework\Event\Observer $observer
     */
    public function execute(Magento\Framework\Event\Observer $observer)
    {
        //Do Something Here
    }
}

In the above example code for Method Injection, note that the execute() method/function in Data class is dependent on the Magento\Framework\Event\Observer class.

Conclusion

Dependency Injection is a great way to reduce tight coupling between application codebase. Rather than hard-coding dependencies, you can inject the list of object that a class may need. Dependency Injection also enables you to manage future changes and other complexity in your codes.

Also, it is essential to follow the design patterns and coding standard while developing in Magento 2. Although, there’s more to learn about this topic, these were only the basics to get you started with Dependency Injection in Magento 2.

If you have any question, please leave me a comment and I will get back to you!

Author Bio

Fayyaz Khattak is a Magento Community Manager at Cloudways – A Managed Magento Hosting Platform. His objective is to learn & share about PHP & Magento Development in Community. Fayyaz is a food lover and enjoys driving. You can email him at [email protected].

Magento Support
& Maintenance Services

Make sure your M2 store is not only in good shape but also thriving with a professional team yet at an affordable price.

Get Started
avada marketing automation
Subscribe

Stay in the know

Get special offers on the latest news from Mageplaza.

Earn $10 in reward now!

Earn $10 in reward now!

comment
iphone
go up