Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento Block construct - use _construct or __construct?

I am a little bit confused. I read Alan Storm's excellent article about Magento Block Lifecycle Methods and as far as I understand one should be using the protected _construct() method to initialize the block. In my case I just want to set the right block template. So I assume I should be using

protected function _construct()
{
    parent::_construct();
    $this->setTemplate('stenik/qaforum/forum.phtml');
}

However, when I look at the blocks of some of the core Magento modules, they seem to use the php __construct method to do it. For example Mage_Poll_Block_Poll, Mage_ProductAlert_Block_Price, Mage_Rating_Block_Entity_Detailed, Mage_Review_Block_Form

Although both ways actually work, I'd like to know what is the right way to do it.

like image 416
Nikolay Dimitrov Avatar asked Aug 19 '13 16:08

Nikolay Dimitrov


1 Answers

It's ultimately academic, but the Right Way To Do It® is to override the Magento constructor i.e. _construct as requested by the core team in Mage_Core_Block_Abstract:

/**
 * Internal constructor, that is called from real constructor
 *
 * Please override this one instead of overriding real __construct constructor
 *
 */
protected function _construct()
{
    /**
     * Please override this one instead of overriding real __construct constructor
     */
}
like image 135
benmarks Avatar answered Nov 08 '22 12:11

benmarks