What is the difference between the various * get helper * methods in Magento?

I have seen several different approaches to getting a specific one helper, and I hope someone can explain the pros and cons of each approach. For example, in template/checkout/cart/sidebar/default.phtmlyou will see both $this->helper('checkout'), and Mage::helper('checkout'). Is there a good reason for these two different methods in the same template?

Below are the various ways to get an assistant that I could find in Magento:

abstract class Mage_Core_Block_Abstract extends Varien_Object
{
โ€ฆ
    /**
     * Return block helper
     *
     * @param string $type
     * @return Mage_Core_Block_Abstract
     */
    public function getHelper($type)
    {
        return $this->getLayout()->getBlockSingleton($type);
    }

    /**
     * Returns helper object
     *
     * @param string $name
     * @return Mage_Core_Block_Abstract
     */
    public function helper($name)
    {
        if ($this->getLayout()) {
            return $this->getLayout()->helper($name);
        }
        return Mage::helper($name);
    }
โ€ฆ
}

class Mage_Core_Model_Layout extends Varien_Simplexml_Config
{
โ€ฆ
    /**
     * Enter description here...
     *
     * @param string $type
     * @return Mage_Core_Helper_Abstract
     */
    public function getBlockSingleton($type)
    {
        if (!isset($this->_helpers[$type])) {
            $className = Mage::getConfig()->getBlockClassName($type);
            if (!$className) {
                Mage::throwException(Mage::helper('core')->__('Invalid block type: %s', $type));
            }

            $helper = new $className();
            if ($helper) {
                if ($helper instanceof Mage_Core_Block_Abstract) {
                    $helper->setLayout($this);
                }
                $this->_helpers[$type] = $helper;
            }
        }
        return $this->_helpers[$type];
    }

    /**
     * Retrieve helper object
     *
     * @param   string $name
     * @return  Mage_Core_Helper_Abstract
     */
    public function helper($name)
    {
        $helper = Mage::helper($name);
        if (!$helper) {
            return false;
        }
        return $helper->setLayout($this);
    }
โ€ฆ
}
+5
source share
1 answer

Mage_Core_Block_Abstract::getHelper()

The method Mage_Core_Model_Layout::getBlockSingleton()does not return a Magento helper object, but rather an instance of a block of type of a Magento object.
I believe that this is deprecated code, for example, the method is Mage::getBlockSingleton()deprecated.

In both cases, a block instance is created from the Magento class identifier.

getBlockSingleton() $_helpers , createBlock() $_blocks.

$_blocks ( ) XML.

getBlockSingleton() , , , , .
() , createBlock(), :

public function alternativeGetBlockSingleton($classId)
{
    foreach (Mage::app()->getLayout()->getAllBlocks() as $block)
    {
        if ($block->getType() == $classId)
        {
            return $block;
        }
    }
    return $this->createBlock($classId);
}

Mage_Core_Block_Abstract::helper()

Mage_Core_Block_Abstract::helper() , Magento .
Mage::helper($name) , .

, $this->helper() , Mage::helper(), (, , ) Mage, Magento , Mage Mage_Core.

, , , , , Mage::helper() , , .

, Magento .

+10

All Articles