Factories are service classes that instantiate non-injectable classes

    Name of the Model class which append word factory becomes factory class.

    Remember: There are three types of classes that are generated in magento2 automatically on Code generation, these classes are found under var/generation in magento2 root folder

    Factory classes

    Interceptor classes

    Proxy Classes

    Lets have a look on below code so i can explain it further

    protected $_productloader;  
      public function __construct(
            \Magento\Framework\View\Element\Template\Context $context,
            \Magento\Catalog\Model\ProductFactory $_productloader
       $this->_productloader = $_productloader;
        ) {
    public function getLoadProduct($id)
            return $this->_productloader->create()->load($id);

    The following example shows a class getting the ProductFactory instance through the constructor:

    Calling the create() method on a factory gives you an instance of its specific class: We can get object by just injecting this class in consturctor but that will create a shared object to create a singleton object we use factory method. So thats it.You create the object of class and than call the method create() which gives you instance of class….Dead simple

    About Author

    You may also like

    No Comment

    You can post first response comment.

    Leave A Comment

    Please enter your name. Please enter an valid email address. Please enter a message.