2013-08-19 30 views
6

我有點困惑。我讀了Alan Storm關於Magento Block Lifecycle Methods的優秀文章,據我所知應該使用protected _construct()方法來初始化該塊。在我的情況下,我只想設置正確的塊模板。所以我想我應該用Magento塊構造 - 使用_construct或__construct?

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

然而,當我看到的一些核心的Magento模塊的塊,他們似乎使用PHP __construct方法來做到這一點。例如Mage_Poll_Block_PollMage_ProductAlert_Block_PriceMage_Rating_Block_Entity_DetailedMage_Review_Block_Form

雖然這兩種方式的實際工作,我想知道是什麼做的正確方法。

回答

8

這是最終的學術,而是正確的方式做IT®是即重寫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 
    */ 
} 
+5

+1正確的意見,但這」不是個t 100%學術 - 當你重載'__construct'方法時,你可以引入不調用父類__construct或者調用父類__construct和刪除構造函數參數的可能性。這可能會在佈局xml更新中使用塊時的行爲方式產生不一致。 –

+0

值得指出,因爲我的答案假設開發者總是做正確的事情®。 – benmarks