Back to Top

Injectable and Non-Injectable Objects in Magento 2

Updated 26 May 2023

In this blog, we are going to learn very important concepts Injectable and Non-Injectable Objects in Magento 2.

What are Injectable Objects?

Injectable objects in Magento 2 are classes that can be instantiated and managed by the Dependency Injection (DI) container. They are typically used for services, repositories, and other core functionalities.

You can also check out our Dependency Injection (DI) blog for a better understanding of DI functionalities.

Here’s an example of an injectable object in Magento 2:

Searching for an experienced
Magento 2 Company ?
Find out More
namespace Vendor\Module\Model;

use Vendor\Module\Api\CustomInterface;
use Vendor\Module\Model\Dependency;

class CustomModel implements CustomInterface
{
    protected $dependency;

    public function __construct(Dependency $dependency)
    {
        $this->dependency = $dependency;
    }

    public function performAction()
    {
        // Use the dependency to perform the action
        $this->dependency->doSomething();
    }
}

In the example above, the CustomModel class has a dependency on the Dependency class, which is injected through the constructor. By following this approach, we can easily manage and replace dependencies, making our code more flexible and maintainable.

What are Non-Injectable Objects?

Non-injectable objects in Magento 2 are those that cannot be automatically instantiated and managed by the DI container. These objects are typically instantiated using the new keyword directly in the code. Non-injectable objects may still have dependencies, but their instantiation is not controlled by the DI container.

Let’s consider an example of a non-injectable object:

namespace Vendor\Module\Model;

use Vendor\Module\Api\CustomInterface;
use Vendor\Module\Model\Dependency;

class CustomModel implements CustomInterface
{
    public function performAction()
    {
        $dependency = new Dependency();
        // Use the dependency to perform the action
        $dependency->doSomething();
    }
}

In the above example, the CustomModel class instantiates the Dependency object directly using the new keyword. This approach tightly couples the classes together, making it difficult to replace dependencies or write unit tests.

When to Use Injectable and Non-Injectable Objects?

It’s recommended to use injectable objects whenever possible, as they promote flexibility, maintainability, and testability. Injectable objects allow for easy swapping of dependencies and facilitate writing unit tests by enabling the use of mock objects.

Hope this will be helpful.

Thanks 🙂

You can see other blogs as well:-

Add Custom Product Attributes

Add Custom Customer Group

Get Dropdown Options From Dependent Dropdown

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home