ObjectManager

Overview

Large applications, such as the Magento application, use an object manager to avoid boilerplate code when composing objects during instantiation.

In the Magento framework, the implementation of the ObjectManagerInterface performs the duties of an object manager.

Magento prohibits the direct use of the ObjectManager in your code because it hides the real dependencies of a class. See usage rules.

Responsibilities

The object manager has the following responsibilities:

  • Object creation in factories and proxies.
  • Implementing the singleton pattern by returning the same shared instance of a class when requested.
  • Dependency management by instantiating the preferred class when a constructor requests its interface.
  • Automatically instantiating parameters in class constructors.

Configuration

The di.xml file configures the object manager and tells it how to handle dependency injection.

This file specifies the preferred implementation class the object manager generates for the interface declared in a class constructor.

The file also specifies whether the object manager should create an object for every request or treat the object as a singleton.

Usage rules

The Magento framework uses the ObjectManager to generate and inject the classes declared in your constructor. Classes should not ask for the ObjectManager itself as a constructor dependency.

You do not call the object manager directly because the framework handles this automatically. Direct use of the create function prevents type validation and type hinting that a factory class provides.

Object creation is also a separate responsibility that should be moved to a dedicated class such as a factory or proxy. In most cases, the framework generates these classes automatically during code compilation.

You may notice in the Magento 2 codebase that some core classes still call the ObjectManager directly.

This code needs porting or exist for backward compatibility purposes.

They are not tacit endorsements of using the ObjectManager directly.

Exceptions

You can depend on and use the ObjectManager class in the following scenarios:

  • You can use the object manager in static magic methods like __wakeup(), __sleep(), etc.
  • You can use the ObjectManager to maintain backward compatibility for a constructor.
  • In a global scope, like in fixtures of integration tests, you can use the object manager.
  • The object manager can be a dependency in classes used for the creation of objects, e.g. factories or proxies.

Related topics