Edit in GitHubLog an issue

Object manager

Large applications, such as the Adobe Commerce and Magento Open Source applications, use an object manager to avoid boilerplate code when composing objects during instantiation.

In Adobe Commerce and Magento Open Source, the implementation of the ObjectManagerInterface performs the duties of an object manager.

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

Adobe Commerce and Magento Open Source use 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.

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 creating objects such as factories or proxies.

Programmatic product updates

If the Access Control List (ACL) rules contain incorrect settings, scripts that update products will fail validation. This results in products assigned to the wrong store view with the wrong status.

To skip ACL validations, use the ObjectManager to set a dynamic dependency injection preference for Magento\Framework\Authorization\PolicyInterface.

Copied to your clipboard
$objectManager = $bootstrap->getObjectManager();
$objectManager->configure([
'preferences' => [
'Magento\Framework\Authorization\PolicyInterface' => 'Magento\Framework\Authorization\Policy\DefaultPolicy'
]
]);
  • Privacy
  • Terms of Use
  • Do not sell or share my personal information
  • AdChoices
Copyright © 2024 Adobe. All rights reserved.