I would like to know how magento is using factory class to create an object i.e, when we use $classFactory->create() function then it return object of the class.

How from which set of code does this whole process is taking place.


By Default create() and get() method is responsible for generate new object and retrieve obejct in magento 2.

Both function are defined in vendor/magento/framework/ObjectManager/ObjectManager.php

When you open above file, You can check create and get function defination. with __construct() defined in above file,

public function __construct(FactoryInterface $factory, ConfigInterface $config, &$sharedInstances = [])
    $this->_config = $config;
    $this->_factory = $factory;
    $this->_sharedInstances = &$sharedInstances;
    $this->_sharedInstances['Magento\Framework\ObjectManagerInterface'] = $this;

Here, $this->_factory is responsible for calling below files, Magento\Framework\ObjectManager\FactoryInterface is used to create dynamic object in magento 2.

Where FactoryInterface is linked to Magento/framework/ObjectManager/Factory/Dynamic/Developer class.

So all logic are resides in below files of create and get factory object,

All Dynamic Object are created from below path,

For Developer mode Object Creation is processed from this file,


For Production mode Object Creation is processed from this file,


Above both file extends Magento\Framework\ObjectManager\Factory\AbstractFactory.php file.

create() function is responsible for create dynamic object of perticular factory.

  • Can you explain little bit more as in magento 1 it was very clear when we were using getsingelton and getmodel, they were called using mage. So I was easily able to understand those function and autoload was responsible for figuring out the path. But Magento2 has confused me, i wanted to explore how it is reaching over here(object manager) from constructor. I hope you understand my query. You can share some link if you know regarding this. – arushi May 25 '17 at 6:37
  • Check my updated answer. – Rakesh Jesadiya May 25 '17 at 6:46
  • Nice explanation Rakesh :) – Amit Bera May 25 '17 at 15:18

Though Rakesh has already given a good explanation. I would like to add my research. I wanted to know how constructor parameter object are created, how factory pattern is working for that:-

$this->_factory in create function of Magento\Framework\ObjectManager\ObjectManager class is an object of Magento\Framework\ObjectManager\Factory\Dynamic\Developer class. In Developer.php we have create function which is getting the constructor parameter using line

$parameters = $this->definitions->getParameters($type);

In class Magento\Framework\ObjectManager\Definition\Runtime we have a function getParameters function which is fetching parameters of constructor.

public function getParameters($className)
    if (!array_key_exists($className, $this->_definitions)) {
        $this->_definitions[$className] = $this->_reader->getConstructor($className);
    return $this->_definitions[$className];

So from here I understand that parameter are being fetched and for object creation:-

In class Magento\Framework\ObjectManager\Factory\Dynamic\Developer, we have return statement as return $this->createObject($type, $args);. Here

$type = classname
$args = list of partameters

Now $this->createObject is an instance of Magento\Framework\ObjectManager\Factory\AbstractFactory

And here it can be clearly seen that objects are created in function

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

To know more for Request flow of magento 2 you can go through this link

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.