-4

Possible Duplicate:
Any reason why Mage::registry(‘current_category’) would return NULL?
Reference - What does this error mean in PHP?

Fatal error: Call to a member function getParentCategory() on a non-object in...

the code:

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

now, when the page can't use getParentCategory() i using the following but can't work.

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

why? thank you

Community
  • 1
  • 1
stackoverflow002
  • 199
  • 1
  • 2
  • 10

3 Answers3

4

It appears that $_category_detail is not an object. Therefore Mage::registry('current_category') is not returning an object.

It's most likely returning some sort of NULL or false value upon fail. And PHP is making you notice that (NULL)->getParentCategory() is meaningless.

In your particular case it returns NULL because current_category is not set in your registry.

Shoe
  • 72,892
  • 33
  • 161
  • 264
2

You need to use method_exists() rather than trying to call a non-existent function:

if (method_exists($_category_detail, "getParentCategory"))
Amadan
  • 179,482
  • 20
  • 216
  • 275
1

isset() only checks for member variables. Use method_exists().

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

if (method_exists($_category_detail, 'getParentCategory')) {
    $id = $_category_detail->getParentCategory()->getId()
}
Hikaru-Shindo
  • 1,881
  • 12
  • 22