Magento 2 is built on a powerful dependency injection (DI) architecture that promotes best practices like loose coupling and easier unit testing. However, many developers encounter a central class known as the Magento 2 Object Manager, which raises some important questions: What is it? When should you use it? And when should you avoid it?
ON THIS PAGE
What is an Object Manager in Magento 2?
-> Magento\Framework\ObjectManagerInterface is Magento 2’s service locator that is responsible for instantiating and managing class dependencies. It enables the dynamic creation of objects, especially when Magento doesn’t know upfront what class it will need (e.g., plugin chains, factories, proxies).

You can get the instance via:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
And then instantiate classes like:
$productRepository = $objectManager->get(\Magento\Catalog\Api\ProductRepositoryInterface::class);
To add Object Manager to the constructor:
/
* @var \Magento\Framework\ObjectManagerInterface
*/
private $objectManager;
/
* @param \Magento\Framework\ObjectManagerInterface $objectmanager
*/
public function __construct(
\Magento\Framework\ObjectManagerInterface $objectmanager
) {
$this->objectManager = $objectmanager;
}
Why Magento 2 Object Manager Exists
The Object Manager is necessary for Magento to wire complex systems together dynamically. It’s also used internally by Magento for bootstrapping and other lower-level tasks.
When You Should Not Use the Object Manager in Magento 2
Direct use of the Object Manager in your custom code (especially in controllers, blocks, models, etc.) is strongly discouraged. Magento’s coding standards (https://developer.adobe.com/commerce/php/development/components/object-manager/) clearly state:
“You should never use Object Manager directly in your code, except in factories, proxies, and framework-level code.”
Instead, you should inject dependencies via the constructor:
public function __construct(
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository
) {
$this->productRepository = $productRepository;
}
When the Magento 2 object manager Can Be Used
There are limited, justifiable cases to use the Object Manager:
1. In scripts or setup files (e.g., setup scripts, install/upgrade scripts, cron jobs, di.xml conditions).
2. In object factories, when you don’t know ahead of time what class to instantiate.
3. For quick testing or debugging, not for production code.

Example of Incorrect Usage of Magento 2 Object Manager
// Avoid this in a controller, block, helper, etc.
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create(\Magento\Catalog\Model\Product::class);
Best Practice: Dependency Injection
Let Magento 2 platform do the heavy lifting through DI. It keeps your code testable, easier to read, and aligned with Magento’s future-proof architecture.
Conclusion
The Magento 2 Object Manager helps Magento create and manage objects, but it’s not meant to be used directly in your custom code. Instead, it’s best to use Dependency Injection to keep your code simple, clean, and easier to manage. Only use the Object Manager in special situations like setup scripts or testing, not in regular code like controllers or models.
By following these tips, your Magento projects will be easier to build, update, and maintain.
Need help with your Magento store? Talk to a Magento solution expert like Klizer or check out our Magento solutions to learn more.