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.
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityI 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,
vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php
For Production mode Object Creation is processed from this file,
vendor/magento/framework/ObjectManager/Factory/Dynamic/Production.php
Above both file extends Magento\Framework\ObjectManager\Factory\AbstractFactory.php
file.
create() function is responsible for create dynamic object of perticular factory.
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