When we do a project, we occasionally appear fatal error because of being cautious. If the display_errors is set to OFF, the user will see a blank page. If set to ON, the fatal error information will be presented (of course, normal people will not do so).
So what can we do to intercept the fatal error in advance and feed back to the user with our own custom friendly form? There is a function called Register_shutdown_function in PHP that lets you set up another function that can be invoked when execution is closed. That is, when our script executes or dies unexpectedly, and the PHP execution is about to close, this function is called.
Let's take a look at the following example:
Copy Code code as follows:
<?php
$flag = false;
function Deal_error () {
Global $flag;
if (! $flag) {
Die ("coarse question, please try again later");
}
return false;
}
Register_shutdown_function ("Deal_error");
will fail because of a fatal error
$obj = new Notexistclass (); Introducing an undefined class
Require ('./test.php ');
$flag = true;
We at the program entrance, set flag to false, and finally set to true, indicating that the program is performing normally. If the flag is not true at the end of the middle, it will call register_shutdown_function and output our custom error results.
If the above class does not define, introduce nonexistent files (must use require or require_once), etc. will cause fatal error. Of course, if your program is missing a punctuation or a special character of what, then there is no way.