1

What types of Injection methods does Magento 2 support?

I am aware about,

  • Constructor Injection - consider as a Injection.
  • Method Injection - consider as a Injection.

BUT

  • And Interface and Model preference from di.xml should be consider as Injection ?

<preference for="Vendor\Module\Api\ManagementInterface" type="Vendor\Module\Model\Management" />

Does it consider as a Injection ?

Note : If you read Constructor injection in Magento dev docs - It says Magento uses the di.xml file to determine which implementations to inject into the Builder class.

3 Answers 3

4

preference from di.xml

definitly not call that injection that's called implementation mapping.

setter injection

yes. that's DI

6
  • What or how many types of Injection methods does Magento 2 support? Commented Jan 22, 2019 at 14:24
  • Method injection and constructor injection. that's it Commented Jan 22, 2019 at 14:26
  • 2
    all described in the devdocs Commented Jan 22, 2019 at 14:26
  • But If you read Constructor injection in Magento dev docs - It says Magento uses the di.xml file to determine which implementations to inject into the Builder class. Commented Jan 23, 2019 at 4:55
  • yes... but the preference key word is not "injection" Commented Jan 23, 2019 at 8:01
1
  1. Constructor Injection

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.

  1. 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.

1

There are three types of Injections Methods Magento2 supports :

  • Constructor Injection
  • Setter Injection
  • Interface Injection
1
  • Where to find setter injection examples or docs? Commented Dec 6, 2021 at 8:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.