Making use of Magento getSingleton method
Posted by Branko Ajzele under Magento @ 9th OCT 2008

In one of my previous articles I showed you how to use getModel and getData methods in Magento. Although we should not put those to methods at the same level, since I’d say the getModel is a bit more higher. Higher in sense, you first use the geModel to create the instance of object then you use getData to retrieve the data from that instance. I have said it before, and I’m saying it again; Magento is a great peace of full OOP power of PHP. It’s architecture is something not yet widely seen in CMS solutions.
One of the architectural goodies of Magento is it’s Singleton design pattern. In short, Singleton design pattern ensures a class has only one instance. Therefore one should provide a global point of access to that single instance of a class.
So why would you want to limit yourself to only one instance? Let’s say you have an application that utilizes database connection. Why would you create multiple instance of the same database connection object? What if you had references to an object in multiple places in your application? Wouldn’t you like to avoid the overhead of creating a new instance of that object for each reference? Then there is the case where you might want to pass the object’s state from one reference to another rather than always starting from an initial state.
Inside the Mage.php file of Magento system there is a getSingleton method (function if you prefer). Since it’s footprint is rather small, I’ll copy paste the code for you to see it.
public static function getSingleton($modelClass=”, array $arguments=array())
{
$registryKey = ‘_singleton/’.$modelClass;
if (!Mage::registry($registryKey)) {
Mage::register($registryKey, Mage::getModel($modelClass, $arguments));
}
return Mage::registry($registryKey);
}
First, notice the word static. In PHP and in other OOP languages keyword static stands for something like “this can be called on non objects, directly from class”. Now let me show you the footprint of the getModel method.
public static function getModel($modelClass=”, $arguments=array())
{
return Mage::getConfig()->getModelInstance($modelClass, $arguments);
}
Do you notice the parameters inside the brackets? They are the same for both of theme. Where am I getting with this? Well, I already showed you how to figure out the list of all of the available parameters in one of my previous articles on my site. So all we need to do at this point is, play with those parameters using getSingleton() method and observe the results.
Most important thing you need to remember is that using getSingleton you are calling already instantiated object. So if you get the empty array as a result, it means the object is empty. Only the blueprint is there, but nothing is loaded in it. We had the similar case using getModel(’catalog/product‘) on some pages. If we done var_dump or print_r we could saw the return array was empty. Then we had to use the load method to load some data into our newly instantiated object.
What I’m trying to say, you need to play with it to get the feeling. Some models will return data rich objects some will return empty arrays. If we were to do Mage::getSingleton(’catalog/session) on view.phtml file we would retrieve an array with some useful data in it. Do not get confused with me mentioning the view.phmtl file, it’s just he file i like to use to test the code. Using Mage::getSingleton(’core/session) would retrieve us some more data. You get the picture. Test, test, test… What’s great about the whole thing is that naming in Magento is perfectly logical so most of the time you will probably find stuff you need in few minutes or so.
For the end here are some screenshots for you to see the results of calling getSingleton with catalog/product and catalog/session parametars.
Hope this will be useful for some of you.
3 Responses to “Making use of Magento getSingleton method”
Leave a Reply
Post Meta
- October 9, 2008
- Magento
- 3 Comments
- Comments Feed
Categories:
- iPhone development (2)
- Magento (17)
- Online Marketing (4)
- Tools (10)
- Wordpress (8)
Recent Posts:
- 19 Nov How to use .swf ads in Go...
- 18 Nov What can you create with ...
- 15 Nov dotProject - Open Source ...
- 10 Nov 5 tips on using blogs to ...
- 08 Nov Advanced search in Magent...
- 06 Nov Online advertisement budg...
- 03 Nov Custom price filter in Ma...
- 02 Nov Getting started with iPho...
- 31 Oct How to track internal sit...
- 30 Oct Revolution 2 is launched


excellent. I can definitely see this helping when you’re calling on data that has already been instantiated. Whereas other times, say, you want to get a different listing of products from a category than the one magento will already be loading, you would then instantiate an entirely new object utilizing the other methods. thanks again for sharing the infos Branko
You are clearly uninformed of what true OOP is. Hopefully we will not see more CMS solutions with this architecture because it is truly a terrible way to do things.
There is nothing Object-Oriented about Magento. It is built on the Zend framework, so a lot of the mess is caused by that. This awful code is what I call “object-based”. Because the data and the methods to act on it are not totally encapsulated, you need kludgy functions like getSingleton and getData, so you can operate on some container of some values. You shouldn’t need to do that in an object-oriented system. This generates a lot of code that is very similar and hard to debug and hard to maintain and has performance issues as well.
Sorry, I have nothing good to say about Magento, except that the concept is nice. The implementation is atrocious. It reminds me of the code I was working on for the U.S. Postal system Point-of-Sale. It was so bad I changed jobs. This code is very similar. Awful!
“You are clearly uninformed of what true OOP is”
I come from .NET (C#) and I belive I have a pretty good understanding of what OOP is. PHP 5 lacks great amount of features as a language to even compare to C#. However, it still functions as OOP language. Hopefuly version 6 will be better.
I agree the code is… hmmm… would not say bad, but disconnected. There is simply to many of the M, V and C-s around. What makes it catastrophic is the full lack of good comments in the code and the general lack of good (aka MS SDK) documentation. System this big needs good IDE, since I can’t imagine any “expert” memorizing all the method names therefore working without code completion features ans so on. Not to mention the debugging which is a big no with PHP.
About the MVC arhitecture itself… I’m not a big fan of it. I find the ASP.Net and it’s code behind files plus the App.config files to do the job just fine. However even ASP.Net has a MVC version nowdays, so I’m guessing there’s something to it.
About the Magento itself… I’m currently loosing my mind with it
I’m always for learning new things, digging into something ans stuff like that but Magento makes the simplest things so hard (stuff like Module development). All I know for now is that one should be Zend Framework master to get thing rolling with Magento.
Hopefully no hard feelings for the things said here
I really appreciate other people views on things, cause no one is perfect. Plus we all sometimes change our views on some topics over time. I entered Magento waters with great enthusiasm, therefore I said a lot of good stuff about it. For the last two weeks I’m breaking my neck over it. So, as some of you might notice I paused my blogging for a while
All the best men. p.s. Feel free to put some comments on my site as well, http://activecodeline.com.
Thank you.