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