Magento Block 构造 - 使用 _construct 还是 __construct?

标签 magento magento-1.7

我有点困惑。我阅读了 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/

相关文章:

php - Magento 管理菜单链接重定向到 404 页面

php - Magento 1.7 客户/帐户 404 错误

magento-1.7 - 我们可以在topmenu中添加不同颜色的下拉属性吗?

javascript - 未识别的数据结构-转换为对象

magento - 使用按钮添加自定义折扣订单

magento - 在 Magento 1.7 中添加新产品时设置默认产品值

css - Magento CE 中定义的 "wide"CSS 类在哪里?

php - Magento 不在 CMS 页面中保存变量或自定义 block

magento - 重写 Magento Mage_Sales_Model_Order_Invoice_Total_Subtotal

php - Magento upsell_products 引发错误 "must be an instance of Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection"