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

Hello all of you today I will explain you what is 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().

$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::getgetModel()

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 .

Hope you like this. 🙂 🙂

Advertisements

2 thoughts on “Difference between Mage::getSingleton() and Mage::getModel() in Magento

  1. Hello Keyul,

    According to your this
    “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().”

    Then why use Mage::getModel() ?

    Can you please us when to use Mage::getModel() and Mage::getSingleton() ?

    Thanks,
    Vaibhav

Leave your comments

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s