Shopping Cart

You have no items in your shopping cart.

30 days money back

We will refund to you if you want to return a product for any reason during 30 days after purchasing it. Read More...

6-month free support included

We guarantee free and fast support during the 6-month term after purchasing a product. Read More...


If you find any bugs in our product during the support term, we will fix them for free. Read More...

Swipe to the right

The Cockoo's Eggs for the helper and singleton

By Igor Goltsov August 8, 2011 4532 Views No comments

There have been a lot of talks about rewrites of some of the standard classes with own as well as about the conflicts of different products that are connected with the issue. We would like to share some experience on the point of redefinition of helpers and singletons.

Practice makes perfect! So now we’re going to extend the function of the class Mage_Core_Model_Design_Package with the change of the work mechanism of one function. This class is used for the View assembly when showing the store. During the rendering of the store the one and only sample of this class is created and the access to it is managed through


The most logical way is to rewrite one of the models by putting the following into the config.xml:


It is a completely correct method that always works until any other module will do the same. In this case who the last will define the rewrite during the rendering it will continue using it.

How we could avoid it? First, we will follow our aim. We need getSingleton () to return our class. So lets us see how precisely it works.

* Retrieve model object singleton
* @param string $modelClass
* @param array $arguments
* @return Mage_Core_Model_Abstract

public static function getSingleton($modelClass='', array $arguments=array())
$registryKey = '_singleton/'.$modelClass;
if (!self::registry($registryKey)) {
self::register($registryKey, self::getModel($modelClass, $arguments));
return self::registry($registryKey);

As you see the method returns the subject to the register the following key (_singleton/<way to the model>) and only if there are no value, then a model is created, written to the register and given from it.

The idea is to writhe the necessary model to the register by ourselves and as early as possible.

In this case we catch the event called "predispatch" and after this we will write our model into it.


Then we write into the Observer.php

    /** Predispatch handler */    
public function predispatch($event)
Mage::register('_singleton/core/design_package', Mage::getModel('modulename/core_design_package'));

That's it. Now by the request


is always returned the sample of our model.

Similar method can be applied to the helpers. The only difference if that  the key in the register is formed as _helper/<name of the helper>.

Adepts of the "clean" programming might criticize this method but in practice it gives us a competitive advantage and a little bit more control over the functionality of the stores where our products are placed.

Benefits in comparison with the standard rewrite through the XML:

  • Depending on the circumstances we can decide whether we should reload the certain class or not (all in all it is the only important benefit)
  • We can reload only front- or back-end.
Posted in: Development