全部博文(315)
分类: 系统运维
2009-11-05 12:48:10
In one of my previous 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 on my . 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.