-4

可能重复:
Mage::registry('current_category') 会返回 NULL 的任何原因?
参考 - 这个错误在 PHP 中是什么意思?

致命错误:在...中的非对象上调用成员函数 getParentCategory()

编码:

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

现在,当页面不能使用 getParentCategory() 我使用以下但不能工作。

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

为什么?谢谢你

4

3 回答 3

4

看起来那$_category_detail不是一个对象。因此Mage::registry('current_category')不返回对象。

它很可能在失败时返回某种NULL或值。false而 PHP 让你注意到这(NULL)->getParentCategory()是毫无意义的。

在您的特定情况下,它返回 NULL 因为current_category未在您的注册表中设置。

于 2012-12-19T08:42:16.497 回答
2

您需要使用method_exists()而不是尝试调用不存在的函数:

if (method_exists($_category_detail, "getParentCategory"))
于 2012-12-19T08:43:37.523 回答
1

isset() 只检查成员变量。使用 method_exists()。

PHP 手册: http: //php.net/manual/de/function.method-exists.php

if (method_exists($_category_detail, 'getParentCategory')) {
    $id = $_category_detail->getParentCategory()->getId()
}
于 2012-12-19T08:43:41.773 回答