Tuesday 28 January 2014

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

You may be in thinking sometime what is the difference between magento getSingleton and getModel method so here is a explanation of the difference The Mage::getModel(‘module/model’) create a instance of model object each time method is got called this approach is resource consuming require separate memory for each object instance.

1. Mage::getModel('catalog/product'), it will create new instance of the object product each time.

2. e.g.
$obj1 = Mage::getModel('catalog/product');
$obj2 = Mage::getModel('catalog/product');
Then $obj1 is a one instance of product and $obj is another instance of product.


While Mage::getSingleton(‘catalog/product’) will create reference of an existing object if any reference is not exists then this method will create an instance using Mage::getModel() and returns reference to it.
So, if
$obj1 = Mage::getSingleton(‘catalog/product’);
$obj2 = Mage::getSingleton(‘catalog/product’);

Then $obj1 and $obj2 both refer to the same instance of product.

No comments:

Post a Comment