ObjectManager in Magento 2

ObjectManager can be called one of the main concepts that underlies the construction of Magento2, and is completely new when compared to Magento1. If we remember Magento1, then, there, to create the objects we need to work, we used the Mage class, which provided static methods for creating different types of objects - be it models, resource models, helpers, or for creating objects that we wanted to have in a single instance (Mage :: getSingleton method). When creating Magento2, the development team abandoned this idea and implemented the principle of dependency injection and service contracts (ServiceContracts). This is what made Magento2 so flexible, easily customizable and testable. Also, the presence of the functionality built around the ObjectManager makes it possible to have all the functionality of customizing the behavior of the system,which we can customize through the di.xml configuration file.



If we look globally at the functionality that the ObjectManager implements, then we can say that it is a kind of implementation of the DI container, which is represented in the PHP world as PSR-11, although the ObjectManager itself does not directly implement Psr \ Container \ ContainerInterface (and does not have a has , which Psr \ Container \ ContainerInterface assumes). It is a centralized facility for creating and retrieving objects. Having such a centralized class for generating the required objects has the following advantages.



  • We do not need to manually initialize and manage objects (we also need to say that the ObjectManager is used to generate objects inside the Factory and Proxy classes, which are created through code generation)

  • it is possible to prescribe through the settings which implementation of a certain interface the class should receive and use the principle of dependency inversion

  • the system becomes easier to test
  • it is possible to use Proxy-classes and factory classes (Factory)
  • Saving server resources, since some of the objects are not reinitialized, but are taken from the cache already created objects (shared setting)


, , Magento\Framework\ObjectManager\ObjectManager protected $_sharedInstances = []. , 1 () — , (namespace). ObjectManager ?



, ObjectManager - shared=true. — shared xml-. (observer).





<observer name="legacy_model_save"
          instance="Magento\Framework\EntityManager\Observer\BeforeEntitySave" shared="false"/>


. , type. , true -, .



ObjectManager . , Http.



index.php, , Magento\Framework\App\Bootstrap

create, 2 :



  • — $_SERVER +


create createObjectManagerFactory, Magento\Framework\App\ObjectManagerFactory Bootstrap, , Magento\Framework\App\ObjectManager. , . Magento\Framework\App\Http::launch ObjectManager  Magento\Framework\App\ObjectManager::configure. di.xml. ObjectManager preference, type, virtualType. ObjectManager — Magento\Framework\ObjectManager\Config\Config, Magento\Framework\ObjectManager\ConfigInterface. di.xml : preference, type, virtualType . preference’ ObjectManager’ .



2 ObjectManager: create get. , create ( , Factory method), get , , .



— (, Magento\Framework\ObjectManager\FactoryInterface), , Magento\Framework\ObjectManager\Factory. php . Magento\Framework\ObjectManager\Factory\AbstractFactory::createObject, . ObjectManager , , .



Magento\Framework\ObjectManager\Code\Generator. , , , Factory Proxy , (Proxy di.xml) . Proxy-, , , proxy-, , ( Proxy). , . Factory factory method, create, ObjectManager Magento\Framework\App\ObjectManager::create.




All Articles