Question

In this code what this customer parameter mean

$this->helper('customer')->getRegisterPostUrl();
Was it helpful?

Solution

First I want to say that you have a lot of question about how Magento works. I like the spirit.

The method helper() that you call from inside a block, that is the same as calling Mage::helper() is a factory.
It returns you the instance of a class. A helper in this case.

The method Mage::helper looks like this:

public static function helper($name)
{
    $registryKey = '_helper/' . $name;
    if (!self::registry($registryKey)) {
        $helperClass = self::getConfig()->getHelperClassName($name);
        self::register($registryKey, new $helperClass);
    }
    return self::registry($registryKey);
}

This means that the class instance you get it will be a singleton. You will get the same class instance every time.

This method recieves a parameter $name. Based on this name Magento identifies the class that needs instatiating. $helperClass = self::getConfig()->getHelperClassName($name);

If you go deeper in the method Mage_Core_Model_Config::getHelperClassName you will see this:

public function getHelperClassName($helperName)
{
    if (strpos($helperName, '/') === false) {
        $helperName .= '/data';
    }
    return $this->getGroupedClassName('helper', $helperName);
}

this means that if the parameter does not contain a slash /data will be added to the parameter.
so $this->helper('customer') is the same thing as $this->helper('customer/data').

If you look further in the getGroupedClassName method...you will probably get lost :). I know I did at first.

But I will explain a bit how it works.
In the config.xml of a module you have under the <global> tag, definitions of models, blocks and helpers. like this

<helpers>
    <module_alias>
        <class>Namespace_Module_Helper</class>
    </module_alias>
</helpers>

When receiving a parameter like customer/data magento looks for the class node: config->global->helpers->customer->class.
In this case it will be Mage_Customer_Helper and appends to it what's after the slash in an uc first form. In this case Data.
So the class name is Mage_Customer_Helper_Data.
This class gets instantiated and the instance is returned.

OTHER TIPS

This is the alias for the Helper class used, this alias is used to build the class that is loaded to run the function specified.

In this case the helper alias for customer maps to Mage_Customer_Helper. Since only the class alias is specified the function getHelperClassName will append /data to load the default Helper class with the name: Mage_Customer_Helper_Data, the autoloader will then load the file Mage/Customer/Helper/Data.php.

Within templates $this->helper() gets translated to Mage::helper()

So this code means this is a call to Mage::helper('customer'), which gets translated to Mage_Customer_Helper_Data (or any class that rewrites it) in the backend.

If you're curious how this is done have a look at Mage_Core_Model_Config::getHelperClassName()

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top