PHP – How to Debug Fatal Error: Call to a Member Function setTitle() on a Non-Object

fatal errorlayoutPHP

I'm getting a

"Fatal error: Call to a member function setTitle() on a non-object in /home/www/public_html/app/code/core/Mage/Customer/Block/Form/Login.php on line 40"

The particular line contains:

 $this->getLayout()->getBlock('head')->setTitle(Mage::helper('customer')->__('Customer Login'));

If I comment out the line the issue is gone – but that is very dissatisfying of course. Since this is a core file it seems something else is causing the issue.

What is the best approach to debug this – or in general a "Call to a member function foo() on a non-object"?

Thank you!

Best Answer

as you already done, the starting point is the line mentioned in the error.
The method setTitle is called from the result of the call ->getBlock('head').
This means that ->getBlock('head') returns something else than an object.
Then check what getBlock should return.
This returns an instance of a block if that one exists in the layout or false if the block does not exist.
This means that the block does not exist in your case.
This can happen for 3 reasons.
The block was never declared in the layout, but this never happens because it is declared in the page.xml file (unless you removed it from there, but I doubt that).
The block was removed via a directive remove through the layout. (possible, but not that probable).
You most probably are using that block before the head block is loaded.
I would look for other occurrences of this block in the layout file and see why it is loaded before the head block.

Side note a bit unrelated: The way this is implemented in the core is wrong. Blocks should not change the title of the page. That should be done in the controller or in the layout files.
Because of this, you are not able to use the same login block to add a small sidebar login on every page, because this way, the title of the page is going to be "Login" on every page.

Related Topic