As long as the program is running, errors will inevitably occur! Sooner or later, it's just a matter of time.
Errors are common, such as Notice, Warning, etc. At this time, set_error_handler is generally used to handle:
set_error_handler(function($errno, $errstr, $errfile, $errline) {
var_dump($errno, $errstr, $errfile, $errline);
});
// Notice: Use of undefined constant strlen
strlen;
// Warning: strlen() expects exactly 1 parameter, 0 given
strlen();
?>
What exactly can be done? Manage error logs in a unified manner, or present a relatively friendly error prompt page, etc.
But it should be noted that set_error_handler cannot catch certain Fatal errors, such as the following error:
set_error_handler(function($errno, $errstr, $errfile, $errline) {
var_dump($errno, $errstr, $errfile, $errline);
});
// Fatal error: Call to undefined function undefined_function()
undefined_function();
?>
But is there really nothing we can do? Of course not, not only do we have methods, but we also have several methods:
The first one: ob_start + error_get_last
ob_start(function($buffer) {
If ($error = error_get_last()) {
return var_export($error, true);
}
return $buffer;
});
// Fatal error: Call to undefined function undefined_function()
undefined_function();
?>
The second type: register_shutdown_function + error_get_last
register_shutdown_function(function() {
If ($error = error_get_last()) {
var_dump($error);
}
});
// Fatal error: Call to undefined function undefined_function()
undefined_function();
?>
In addition, all Parse errors cannot be caught, but from another perspective, the code itself for parsing errors should not be released, or even enter the repository. Regarding this, I have previously written an article "Subversion Hooks", which introduces Learn how to use Subversion hooks to check code syntax.
It seems that I should follow the current trend and write some text to introduce the abnormality. Unfortunately, it is getting late, so I might as well go to bed.
Author Lao Wang