Factory classes code generation Factory Design Pattern Magento 2
Factory classes code generation Factory Design Pattern Magento 2
Factory classes code generation Factory Design Pattern Magento 2
Factory classes is most useful let discuss how it work in magento 2,
factory methods we need to instantiate an object
factory word which append after model class and we do not need to do nothing magento autometic generate factory class code in var / generation folder
whenever magento encounters the class name of end of factory.
So basically Factories are service classes that instantiate non-injectable classes,models that represent a database entity.
Factory classes are generated when code generation happens.
They are created automatically for models that represent database entities.
Factory classes are used to create, get, or change entity records without using the ObjectManager directly,
These classes are generated in the folder var/generation in magento2 root folder.
To instantiate a model object we will use automatic constructor dependency injection to inject a factory object,
and then use factory object to instantiate the model object.
There are three types of classes that are generated in magento2,
Factory classes
Interceptor classes
Proxy Classes
function __construct ( \Magento\Cms\Model\BlockFactory $blockFactory) {
$this->blockFactory = $blockFactory;
}
$block = $this->blockFactory->create();