If you’ve been working with Magento 2, chances are you’ve come across the term Object Manager.
It looks tempting, right? With just one line of code, you can create any class instance:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$myModel = $objectManager->create(\Vendor\Module\Model\Example::class);Easy and quick. But here’s the catch – Magento strongly recommends not to use Object Manager directly in your code.
Let’s understand why, in plain simple words.
Why Not Use Object Manager?
1. Breaks Dependency Injection (DI) Principle
Magento 2 is built on top of Dependency Injection. Instead of creating objects yourself, Magento automatically gives you the dependencies you need via constructor injection:
class MyClass
{
private $example;
public function __construct(
\Vendor\Module\Model\Example $example
) {
$this->example = $example;
}
}This way:
- Your class is easy to test.
- You know exactly what it depends on.
- No hidden objects are created inside.
If you use Object Manager directly, you hide those dependencies, which makes your code harder to maintain and test.
2. Hard to Test
Imagine you want to run a unit test.
If your class relies on Object Manager, it will always create the real object. You can’t pass a mock or fake dependency easily.
With proper DI, you can replace the dependency during testing without touching the original code. This makes your code test-friendly.
3. Not Future-Proof
Magento’s core team has clearly mentioned that Object Manager is for internal framework use only.
If you use it directly, you’re going against Magento’s coding standards.
Tomorrow, if Magento changes how Object Manager works internally, your code might break.
4. Code Smell = Quick Fix, Not a Solution
Most of the time, developers use Object Manager when they want a quick solution instead of properly injecting dependencies.
This creates “hidden shortcuts” in the codebase, making it messy over time.
Clean code today saves you a lot of debugging headaches tomorrow.
If you see $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); in your code, it’s a red flag. 🚩
Keep your Magento code professional, maintainable, and aligned with best practices.



