public function __construct(
    \Magento\Catalog\Helper\Category $categoryHelper,

Let's say we have code like that. How does Magento know to inject category helper and lets us use it? It works like total magic.


The new object is created in lib/internal/Magento/Framework/ObjectManager/Factory/AbstractFactory.php:93

protected function createObject($type, $args)
    return new $type(...array_values($args));

You can see how these arguments are created and cached in lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php:88

How are the constructor arguments read?

During the bootstrap initialization /var/di/{area}.ser files are loaded. These are previously created by the setup:di:compile command and they are basically serialized strings that contain argument relations.

I hope it answers your question to some extent :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.