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

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

Mage::getModel()

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

Example

1
2
$prod1 = Mage::getModel('catalog/product');
$prod2 = Mage::getModel('catalog/product');

$prod1 and $prod2 both have different instant of same object and also occupy different memory .

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().

1
2
$prod1 = Mage::getSingleton('catalog/product');
$prod2 = Mage::getSingleton('catalog/product');

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

Leave a Reply

Your email address will not be published. Required fields are marked *