Magento getSingleton confusion

I'm a little confused about calls I see to Mage::getSingleton, and I'm hoping someone can help me understand a little better.

I have seen a piece of core code that does this:


I don't know PHP, but I think I can make a safe assumption from the getSingleton method name that there will be only one instance of the class specified (the class being specified as a grouped class name, and resolving to app/code/core/Mage/Customer/Model/Session.php - containing class Mage_Customer_Model_Session.

Pregunta 1 -

How did the getSingleton method know to look in the Model folder for the class?

Pregunta 2 -

So there is one instance of the class for the whole ... I want to say JVM as I am from a Java background, but I'll say PHP engine in the hope that that is vaguely the correct terminology; the Mage_Customer_Model_Session is not passed in a customer id or any such identifier, yet we call the method isLoggedIn()! Give there is not a Mage_Customer_Model_Session instance per customer, how can we ask a singleton if a customer is logged in when we do not tell it what customer we are talking about?

Pregunta 3 -

I've seen calls to Mage::getSingleton('core/session') y para Mage::getSingleton('customer/session') - what is the difference?

Gracias por cualquier ayuda.

preguntado el 08 de noviembre de 11 a las 11:11

2 Respuestas

First, before we get to Magento, it's important to understand that PHP has a radically different process model than Java.  A PHP singleton (regardless of Magento's involvement) is a single instance of a class per HTTP Request.  A PHP program isn't persistent in memory the same way a Java program is, so adjust your expectations of a "singleton" accordingly.   

Next, it's important to understand that Magento is a framework built en la parte superior of PHP, using PHP, and in many cases the original Magento developers wanted to push things towards a more Java like architecture.  So, you're going to see things that look familiar, are familiar, but likely differ in some major way from what you're used to because they still need to hew to PHP's version of the universe. 

Magento uses a factory pattern to instantiate Helpers, Blocks, and "Model" classes.  The string


is a class alias.  This alias is used to lookup a class name in Magento's configuration. In short, this string is converted into path expressions that search Magento's configuration files to derive a classname, based on the context (helper, block, model) it was called in. For a longer version, see my Magento's Class Instantiation Autoload artículo.

The concept of a "Model" is a little fuzzy in Magento.  In some cases models are used as domain, or service models.  In other cases they're used as a more traditional middleware database persistence models.  After working with the system for a few years, I think the safest way to think about Models is they're Magento's attempt to do away with direct class instantiation.

There's two ways to instantiate a model class. 


The first form will get you a new class instance.  The second form will get you a singleton class instance.  This particular Magento abstraction allows you to create a singleton out of cualquier Magento model class, but only if you stick to Magento's instantiation methods.  That is, if you call 


then subsequent calls to 


will return that singleton instance.  (This is implemented with a registry pattern). However, there's nothing stopping you from directly instantiating a new instance of the class with either

$o = Mage::getModel('groupname/classname');
$o = new Mage_Groupname_Model_Classname();

Which brings us to sessions.  PHP's request model, like HTTP, was originally designed to be stateless.  Each request comes into the system with, and only with, information from the user.  As the language (and the web) moved towards being an application platform, a system that allowed information to be persisted was introduced to replace the homegrown systems that were cropping up.  This system was called sessions.  PHP sessions work by exposing a super global $_SESSION array to the end-user-programmer that allow information to be stored on a per web-user basis.  Sessions are implemented by setting a unique ID as a cookie on the user end, and then using that cookie as a lookup key (also standard practice for web applications)

In turn, the Magento system builds an abstraction on top of PHP's session abstraction.  In Magento, you can create a "session model" that inherits from a base session class, set data members on it, and save/load those data members just as you would with a database persistence model.  The difference is information is stored in the session instead of the database store. When you see


these are two different session models, with each one storing different data. One belongs to the Mage_Core module, the other belongs to the Mage_Customer model.  This systems allows modules to safely set and manipulate their own session data, without accidentally stepping on another module's toes, and provide logical class methods for manipulating that data.

Hopefully that answers the questions you asked, as well as the ones you didn't.

Respondido 01 Jul 15, 18:07

Thank you very much for taking the time to reply so fully. That was very informative. - user265330

Alan, great explanation - but the info about an initial call to getSingleton() followed by calls to getModel() is not quite accurate. In violation of the Singleton pattern, the second call (to getModel()) in a given execution scope will return a completely new instance. Subsequent calls to getSingleton() however will return the object singleton as described. - Benmarks

Geez, wish I had have been able to read this a few years ago when I first started working with Magento. Great write-up Alan. - Shaune

@sdek I wish I'd been able to read it a few years ago as well! - Alan Storm

I read this answer and halfway through thought 'Who the hell would know this much about Magento?' and of course I was not surprised when I saw the answerer's identity. Great answer. - Siliconrockstar

  1. Magento's getSingleton is almost the same as getModel. The difference is getModel always returns a new instance of a class, and getSingleton creates a new instance of a class only once and then always returns this instance. See the Mage::getSingleton y Mage::getModel methods. Magento knows about looking to the Model folder because of configs in the config.xml file (f.e. Mage/Customer/etc/config.xml) Ver el Magento wiki for developers to know more about config files.

  2. You do not specify customer directly. It's done automatically by Magento in parent classes of Mage_Customer_Model_Session (consulte Mage_Core_Model_Session_Abstract_Varien::start() método)

  3. Magento has not one session class to discriminate session data. For example, customer ID is stored in Mage_Customer_Model_Session and error flash message 'Product is not available' will be stored in the Mage_Catalog_Model_Session clase.

respondido 08 nov., 11:15

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.