Chat on WhatsApp

What does Magento mean by Dependency Injection, and why is it different from "just use new"?

Magento's DI container is in Magento\Framework\ObjectManager. You never call new Vendor\Module\Foo() in production code — you type-hint the dependency in your constructor and the container auto-wires it, applying any preference, plugin chain, or virtualType from etc/di.xml. This is what lets a third-party module plug your service without changing your code. Two flavours: Constructor Injection (preferred, type-hinted, eager) and Method Injection via ObjectManager::create() (only legal in factories, repositories, and setup scripts). Calling ObjectManager::getInstance() in a controller is the Magento equivalent of a code smell — reviewers will fail the PR.

Was this helpful?