Skip to content

Instantly share code, notes, and snippets.

@1234ru
Last active February 21, 2019 22:50
Show Gist options
  • Save 1234ru/a5b5923b2d20abdaa18eb3c8e55750f0 to your computer and use it in GitHub Desktop.
Save 1234ru/a5b5923b2d20abdaa18eb3c8e55750f0 to your computer and use it in GitHub Desktop.
A small wrapper around PHP's built-in function for custom error handling.
<?php
/**
* A small wrapper around PHP's built-in function for custom error handling.
*
* @param callback $callback this function accepts four parameters, same as required for set_error_handler() callback.
* For error types explanation see http://php.net/manual/en/errorfunc.constants.php
*
* @return void
*/
function set_real_error_handler($callback) {
$wrapper = function($type, $message, $file, $line) use ($callback) {
$callback($type, $message, $file, $line);
};
set_error_handler($wrapper);
register_shutdown_function( function() use ($wrapper) {
$error = error_get_last();
if (
$error
AND
in_array(
$error['type'],
[ E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING ]
)
)
$wrapper($error['type'], $error['message'], $error['file'], $error['line'] );
} );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment