2012-12-19 20 views
-4

Possible Duplicate:
Any reason why Mage::registry(‘current_category’) would return NULL?
Reference - What does this error mean in PHP?如何禁止致命錯誤:調用成員函數?

致命錯誤:調用一個成員函數getParentCategory()一個非對象在...

代碼:

$_category_detail=Mage::registry('current_category'); 
$id=$_category_detail->getParentCategory()->getId(); 

現在,當頁面無法使用getParentCategory()我使用以下,但不能工作。

if(isset(getParentCategory()){ 
     $id=$_category_detail->getParentCategory()->getId(); 
    } 

爲什麼?謝謝

回答

2

您需要使用method_exists(),而不是試圖調用一個不存在的功能:對成員變量

if (method_exists($_category_detail, "getParentCategory")) 
+0

如果'$ _category_detail'不是一個有效的對象不觸發它的錯誤? – Shoe

+0

它不會 - 只是自己測試它...文檔也不測試或觸發錯誤;)只是返回'false' –

4

看來$_category_detail不是一個對象。因此Mage::registry('current_category')不返回對象。

這很有可能在失敗時返回某種NULLfalse值。而PHP讓你注意到(NULL)->getParentCategory()沒有意義。

在您的特殊情況下,它會返回NULL,因爲current_category未在註冊表中設置。

相關問題