Magento Helper – Referencing a Helper Class in Magento

helpermodule

I am trying to write a helper class for my module. The helper class will provide two methods with various functionality. Can I put this helper class beside the other helper class in my …/Helper/Data.php file or should I put it somewhere else? Also, how should I reference it from other parts of Magento in order to get access to its methods?

Best Answer

You can create as many helpers as you want in your module.
Let's say your module name is Namespace_Module.
The config.xml of your module should contain this inside the <global> tag:

<helpers>
    <module>
       <class>Namespace_Module_Helper</class>
    </module>
</helpers>

Now let's say you want to create a helper called Something.
You should place the code in Namespace/Module/Helper/Something.php:

<?php 
class Namespace_Module_Helper_Something extends Mage_Core_Helper_Abstract { //or any other helper

    public function doSomething(){
        ....
    }
}

You can call your new helper anywhere like this:

Mage::helper('module/something')->doSomething();

Basically Mage::helper('module'); is the same thing as Mage::helper('module/data');

Related Topic