How to Instantiate a Model in Magento 2 How to Instantiate a Model in Magento 2

How to Instantiate a Model in Magento 2

We all know that in Magento 1 We can easily instantiate model using Mage::getModel and Mage::getSingleton methods. But instantiate a model in Magento 2 something different.

In Magento 2 we can instantiate a model using Object Manager. Yes, you heard it correctly, ObjectManager is a replacement for getModel and getSingleton Method.

There are two approaches you can invoke ObjectManager in Magento 2 to instantiate a model or to get the model collection.
 

Approach 1: Using ObjectManager Directly anywhere

Let’s say we want to get product collection in our Magento 2 custom module phtml file. You can get this using below code.

<?php 
 $objectManager = MagentoFrameworkAppObjectManager::getInstance();
 $productCollection = $objectManager->create('MagentoCatalogModelResourceModelProductCollectionFactory');
 $productCollection->addAttributeToSelect('*');
 $productCollection->load();
 foreach ($productCollection as $product) {
    echo "<pre>";
    print_r($product->getData());     
    
}

 

Approach 2: Inject model into Constructor and instantiate

Second and recommended approach of instantiating a model in Magento 2 is using a constructor. To get product collection in your custom phtml file, You need to create a block file inject the product model into the constructor of that block file.
 
Step 1: Create Block.php file
For example our block file Index.php is under app/code/Codextblog/Pcollection/Block/Index

<?php
namespace CodextblogPcollectionBlockIndex;
class Index extends MagentoFrameworkViewElementTemplate {

protected $_productCollectionFactory;

public function __construct(
    MagentoBackendBlockTemplateContext $context,
    MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory,
    array $data = [] 
) 
   {
    $this->_productCollectionFactory = $productCollectionFactory;    
    parent::__construct($context, $data);
   }
   public function getProductCollection()
    {
        $collection = $this->_productCollectionFactory->create();
        $collection->addAttributeToSelect('*');
        return $collection;
    }
}

 
Step 2: Call getProductCollection in your phtml file
For example our phtml file Index.phtml is under app/code/Codextblog/Pcollection/view/frontend/templates

<?php
$pCollection = $block->getProductCollection();
foreach ($pCollection as $product) {
    echo "<pre>";
    print_r($product->getData());     
    
}

 
This is how you can get product collection in your custom phtml file.
 

Which Approach to use?

Well, it is a very tedious task to go with approach 2 to just get product collection. But as per Magento Official doc, you should always use constructor method to instantiate a model in Magento 2 to maintain the implementation of Dependency Injection.

If you liked this post, then please like us on Facebook and follow us on Twitter.

Written by

Chirag

Chirag is a Founder & eCommerce Technology Leader at MageArmy. He leads the eCommerce development team and drives the company’s technology vision, helping businesses build, scale, and optimize high-performing online stores. With years of experience in eCommerce development, he has worked with businesses across industries to solve complex technical challenges and create scalable digital commerce solutions. He loves sharing practical insights about eCommerce development, Magento, Adobe Commerce, and the latest trends in digital commerce to help online businesses grow and succeed.