Home » Blog

Dependency Injection Magento2

Dec 14, 2020     5 min read

In programming languages, a dependency is known as the object which is needed by a class to perform some functions. Injection is the passing of that particular dependency to a dependent object/class. The real meaning of Dependency Injection is to inject the dependency into the class from another source.

For example, if you have defined a Class in Magento 2 that fetches some data by using Magento 2 Helper Class, we can say that your Class has a dependency of that Magento 2 Helper Object.

By using Automatic Dependency Injection (ADI), the object doesn’t need to look for another dependent object. There are three types of Automatic Dependency Injections:

Magento 2 uses Automatic Dependency Injection (ADI) which is further derived to the Constructor Injection. It means in Magento 2, passing the objects in the constructor’s parameters will help you follow the injection rules. Dependency Injection in Magento 2 is an alternative to the Mage Class method of Magento 1. Below example will give you an idea that how it will look like in Magento 2.

Helper Class in Magento 2 (Data.php):

namespace Namespace\Module\Helper;
class Data extends Magento\Framework\App\Helper\AbstractHelper
{
    public function HelperFunc()
    {
        return 1;
    }
}

Calling HelperFunc() using DI:

class ClassName
{
    public function __construct(Namespace\Module\Helper\Data $helper)
    {
        $this->helper = $helper;
    }
    public function func()
    {
        $this->helper->HelperFunc();
    }
}

In above code class, you can see how Dependency is injected into the constructor of the class. You can use this method anywhere in Magento 2.