Why is my custom error handler not called?

When my script runs, I have:

error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

And then I register my error handler with PHP:

function handleError($code, $text, $file, $line) {
    echo "&%!!";
    return true;
}

set_error_handler('handleError');

Next, the code that causes the error appears:

Fatal error: method undefinedDB :: getInstanceForDB () was called in /Applications/MAMP/htdocs/mysite/classes/Test.php on line 32

I continue to receive a standard PHP error message box with a call stack and everything on my site, regardless of whether I set a special error handler or not. Any idea what's wrong?

Edit: regardless of whether I return true or not, it does not call my custom handler.

+3
source share
3 answers

true. set_error_handler:

FALSE, .

-, , set_error_handler. register_shutdown_function. :

// Handles non-fatal errors
function handleError($code, $text, $file, $line) {
    var_dump($code);
    return true;
}
set_error_handler('handleError');

// Handles fatal errors
function fatalShutdown() {
    var_dump(error_get_last());
}
register_shutdown_function('fatalShutdown');
+5

, , , , .

, set_exception_handler set_error_handler:

// from http://www.php.net/manual/en/function.set-error-handler.php
define('FATAL', E_ERROR | E_PARSE | E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_COMPILE_WARNING);

register_shutdown_function('shutdown');

// Handles "fatal" errors e.g. Syntax errors
function shutdown() {
    // Only if there was an fatal error, this is run on all execution endpoints
    $error_info = error_get_last();
    if ($error_info !== null && ($error_info['type'] & FATAL)) {
        # stack trace set to empty array, as generating one here is useless
        [[ do stuff like emailing someone]]
    }
}
+3

, .

, , ... ... .

set_error_handler:

: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, E_STRICT, , set_error_handler().

0
source

Source: https://habr.com/ru/post/1726676/


All Articles