php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #51848 Non-object method call errors should be catchable with set_error_handler()
Submitted: 2010-05-18 10:10 UTC Modified: 2014-10-07 22:11 UTC
Votes:4
Avg. Score:5.0 ± 0.0
Reproduced:3 of 3 (100.0%)
Same Version:1 (33.3%)
Same OS:1 (33.3%)
From: jelle at gmta dot nl Assigned: thekid (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: Irrelevant OS: N/A
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: jelle at gmta dot nl
New email:
PHP Version: OS:

 

 [2010-05-18 10:10 UTC] jelle at gmta dot nl
Description:
------------
Calling member functions on non-object variables fails with a fatal error. This error is not catchable using PHP's internal error handling configured using set_error_handler(). See the test script below for an example.

I think error handlers should be able to catch this problem. We use a lot of ORM in our applications which involves a lot of object getting, and sometimes we forget to check whether we really have an object. We rely on PHP's error handling to tell us exactly what's going on but cannot use this functionality at this moment.

I believe some errors are not handled by the custom handler because of the unknown or unstable state the engine resides in after the the error. For this case, it's true as the desired method execution / code jump never takes place. I think this can be solved (for this particular error) by stopping execution after calling the custom handler.

Bug #12136 (closed) describes this problem but describes it as a design feature. I think this design can be improved a bit.

Test script:
---------------
<?php

function handleError($errno, $errstr, $errfile, $errline, $errcontext) {
        print_r(func_get_args());
        exit();
}

set_error_handler('handleError');

$a = NULL;
$a->nonExistingMethod();

Expected result:
----------------
The custom error handler function arguments as per print_r(func_get_args()).

Actual result:
--------------
Fatal error: Call to a member function nonExistingMethod() on a non-object in fatalErrorHandling.php on line 11

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-12-01 15:36 UTC] jani@php.net
-Package: Unknown/Other Function +Package: Scripting Engine problem
 [2014-10-07 22:11 UTC] thekid@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: thekid
 [2014-10-07 22:11 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

Fixed in PHP 7.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 19:01:31 2024 UTC