Home / Magento / Difference between Mage::getSingleton() and Mage::getModel() in Magento

Difference between Mage::getSingleton() and Mage::getModel() in Magento

Mage::getSingleton()

Mage::getSingleton() will first check the same class instance is exits or not in memory. If the instance is created then it will return the same object from memory. So Mage::getSingleton() faster then Mage::getModel().

Select Code
$product1 = Mage::getSingleton('catalog/product');
$product2 = Mage::getSingleton('catalog/product');


$product1 and $product2 both will share same memory of OS and return only one instance each time.

Mage::getModel()

Mage::getModel() will create a new instance of an object each time even such object exists in configuration.

Example

Select Code
$product1 = Mage::getModel('catalog/product');
$product2 = Mage::getModel('catalog/product');


$product1 and $product2 both have different instant of same object and also occupy different memory .

When we call the function Mage::getSingleton(‘catalog/product’) magento will search in the memory whether there is any object available. If not it will create a new object for Mage_catalog_Model_product class. In the first iteration of the foreach loop this happens. But from the second iteration when magento searches in memory for Mage_catalog_Model_product class object it will find the object which was called in the first iteration. So magento won’t create any new object and instead it will call the same object which is already in the memory.
But, If we use Mage::getModel(‘catalog/product) this function always creates new object of Mage_catalog_Model_product class in the memory whenever you called it. So in the loop this function will create one object per iteration.

About v.shakya

I am V.Shakya, Software Developer & Consultant I like to share my ideas, views and knowledge to all of you who come across my website. I am young, enthusiastic, highly motivated and self disciplined person. I completed my studies in Master of Computer Application and currently giving my technical expertise to one of the Big IT company. I have more than fifteen years of experience in vast field of Programming , Designing and Development of websites and various software's.

Check Also

How we can add one more new option in product images in magento?

You would need to update following files : 1) appcodecoreMageCatalogModelProductAttributeBackendMedia.php 2) appcodecoreMageCatalogModelResourceProductAttributeBackendMedia.php 3) appcodecoreMageCatalogModelProductAttributeMediaApi.php 4) …

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.