我有点困惑。我阅读了 Alan Storm 关于 Magento Block Lifecycle Methods 的优秀文章据我了解,应该使用 protected _construct()
方法来初始化 block 。就我而言,我只想设置正确的 block 模板。所以我想我应该使用
protected function _construct()
{
parent::_construct();
$this->setTemplate('stenik/qaforum/forum.phtml');
}
但是,当我查看一些核心 Magento 模块的 block 时,它们似乎使用 php __construct 方法来完成。例如
Mage_Poll_Block_Poll
, Mage_ProductAlert_Block_Price
, Mage_Rating_Block_Entity_Detailed
, Mage_Review_Block_Form
尽管这两种方法实际上都有效,但我想知道正确的方法是什么。
最佳答案
它最终是学术性的,但正确的做法是重写 Magento 构造函数,即 _construct
根据 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
*/
}
关于Magento Block 构造 - 使用 _construct 还是 __construct?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18318781/