php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54195 The notorious "Call to a member function .. on a non-object" fatal error
Submitted: 2011-03-08 15:40 UTC Modified: 2014-10-08 06:48 UTC
From: landeholm at gmail dot com Assigned: thekid
Status: Closed Package: Scripting Engine problem
PHP Version: 5.3.5 OS: Irrelevant
Private report: No CVE-ID:
 [2011-03-08 15:40 UTC] landeholm at gmail dot com
Description:
------------
I had this problem a million times. It's very easy to accidentally invoke "Fatal Error: Call to a member function .. on a non-object". The problem is that this triggers an error so fatal that it can't even be caught by the shutdown function. This recently gave me a huge headache in a production system where an obscure bug where a variable contain null which was called on invoked a silent crash. It's a headache because I run everything in a framework with great wrappers for error handling/detection that are suppose to send me an email when obscure bugs get tripped. This obviously doesn't work when the PHP commits seppuku and explodes.

I don't see any reason for this error to be that fatal. Sure, keep the error fatal but at least allow the shutdown function to catch it.

Test script:
---------------
\register_shutdown_function(function(){
    $e = \error_get_last();
    if (!\is_null($e))
        die('Houston we have a problem: ' . \print_r($e, true));
});

$hello = null;
$hello->bar();

Expected result:
----------------
Houston we have a problem: Array
(
...

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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-03-09 17:05 UTC] tyra3l at gmail dot com
I've mentioned this on the list, but to be archived:
AFAIK register_shutdown_function should be called on every errors, even on the fatal ones, and it does.
at least for me, both on windows, and linux:

tyrael@devel-tyrael:~/c$ php -f fatal.php
PHP Fatal error:  Call to a member function bar() on a non-object in /home/tyrael/c/fatal.php on line 9
PHP Stack trace:
PHP   1. {main}() /home/tyrael/c/fatal.php:0
Houston we have a problem: Array
(
    [type] => 1
    [message] => Call to a member function bar() on a non-object
    [file] => /home/tyrael/c/fatal.php
    [line] => 9
)

as you can see from the output of the example code provided by Hannes Landeholm, it does called on the fatal error.

if it doesn't work for Hannes, then I think that there are some difference in our setup/configuration.

Tyrael
 [2011-03-09 17:14 UTC] landeholm at gmail dot com
-Status: Open +Status: Closed
 [2011-03-09 17:14 UTC] landeholm at gmail dot com
NVM. These errors are catchable by the shutdown function. I tested in a context with errors suppression enabled.
 [2011-03-09 18:13 UTC] cataphract@php.net
-Status: Closed +Status: Bogus
 [2014-10-08 06:48 UTC] thekid@php.net
-Status: Not a bug +Status: Closed -Assigned To: +Assigned To: thekid
 [2014-10-08 06:48 UTC] thekid@php.net
The fix for this bug has been committed.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.

 For Windows:

http://windows.php.net/snapshots/
 
Thank you for the report, and for helping us make PHP better.

https://wiki.php.net/rfc/catchable-call-to-member-of-non-object

Now in PHP 7
 
PHP Copyright © 2001-2015 The PHP Group
All rights reserved.
Last updated: Fri May 22 10:01:54 2015 UTC