Get Free Audit

Magento 2 Dependency Injection

Mar 13, 2018

2212 Andrey Litvin

Magento 2 Dependency Injection

The article is believed to be useful for PHP developers who are mastering Magento 2 components. You will learn dependency inversion principle, functions of object manager and dependencies compiling. In case you have some questions, feel free to ask our certified developers for help.

Dependency injection is a design pattern based on the “inversion of control” principle. It involves objects relations to their dependencies. Instead of creating dependencies manually, all necessary dependencies are rendered to the object via the container. The approach helps to avoid excessive code coherence, as now the object is not required to its own dependencies but IoC container defines which functions are to be rendered to the object depending on configuration.

Dependency inversion principle states that high-level classes must use the abstractions of low-level objects but not deal with them directly. It also reduces code coherence and mitigates non-compatibility risk in case if object implementation is changed.

Object Manager

Magento 2 uses dependency injection for functionality that was represented by Mage in Magento 1.

ObjectManager is used as a IoC container. It is responsible for:

  • creating objects in factories and proxies.
  • implementing singleton pattern returning the same object instance on subsequent requests.
  • selecting a necessary implementation on interface request.
  • creating classes automatically focusing on the arguments of the constructor.

ObjectManager is configured using di.xml files. These files contain the preferred interface implementation and also it shows ObjectManager to use the object as singleton or create a new instance every time it’s necessary. Each module can have global as well as area-specific di.xml files. Area-specific di.xml files should be used to configure dependencies for presentation layer when global di.xml files are for the rest.

Abstraction-implementation mappings and class rewrites

It is used when the constructor asks the object for its interface. ObjectManager uses the configuration when deciding which implementation should be executed for the current scope.

Partner With Us Let's discuss how to grow your business. Get a Free Quote.
Talk to Andrey

The same approach can be used not only for replacing interface but for classes.

Virtual Types

It allows changing the arguments of any dependency and hereby modifying the class behavior without affecting other classes that are dependent on the original.

ObjectManager should never be called directly, as Magento Framework does it automatically. The exception can be factory or proxy classes which are in most cases generated by framework automatically or unit tests and static magic methods (wakeup, sleep).

Magento 2 code contains direct ObjectManager calls but they shouldn’t be used as an example and exist only for backward compatibility.

Compiling dependencies

Magento 2 uses a special utility to compile the dependencies of all classes. It compiles the file that contains the dependencies of all objects for ObjectManager based on constructor arguments with the help of php reflection features. Service classes such as factories, plugins, and proxies are also generated.

Andrey Dubina
Partner With Us Looking for a partner to grow your business? We are the right company to bring your webstore to success. Talk to Andrey

Post a new comment

BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *