Magento 2 uses Dependency Injection to replace functionality provided by the Mage class in Magento 1.x.

Dependency Injection is a design pattern that allows object A to declare its dependencies to an external object B that supplies those dependencies. The dependencies declared by A are usually class interfaces, and the dependencies B provides are concrete implementations for those interfaces.

This allows for loose coupling of code because object A no longer needs to be concerned with initializing its own dependencies. Object B decides which implementations to provide to object A based on a configuration or desired behavior.

This is an important concept to understand for extension developers because it forms the basis of how Magento composes its classes.

Object manager a DI container

The ObjectManager is a Magento service class that instantiates objects at the beginning of the bootstrapping process.

Magento uses class constructor signatures to retrieve information about an object’s constructor dependencies. When a class is constructed, the object manager injects the class’s dependencies, defined in the di.xml file into the class constructor.

Since the object manager provides its service indirectly, your class should not depend on the ObjectManager object itself. The only exceptions are custom factories with complex logic and integration tests that need environment setup.

#php #magento

Magento 2 (DI)Dependency Injection Hell Architecture
2.00 GEEK