This article describes how to use the register_shutdown_function function in PHP to intercept fatalerror. For more information, see fatal error. If display_errors is set to off, the user will see a blank page. If it is set to on, the fatal error information will be displayed (of course normal people will not do this ).
Then, how can we intercept the fatal error in advance and provide it to the user in our custom friendly form. PHP has a function called register_shutdown_function. We can set another function that can be called when the function is disabled. that is to say, this function will be called when PHP Execution is about to close when the script execution is completed or accidentally killed.
The following is an example:
The Code is as follows:
<? Php
$ Flag = false;
Function deal_error (){
Global $ flag;
If (! $ Flag ){
Die ("rough problem, please try again later ");
}
Return false;
}
Register_shutdown_function ("deal_error ");
// Will fail due to a fatal error
// $ Obj = new NotExistClass (); // introduce undefined classes
Require ('./test. php ');
$ Flag = true;
At the program entrance, we set flag to false, and finally to true, indicating that the program runs normally. If the flag is not true at the end, it means that it is dead somewhere in the middle. At this time, register_shutdown_function will be called to output our custom error results.
The above class is undefined and introduction of non-existent files (require or require_once must be used) will cause fatal error. Of course, if your program lacks punctuation or special characters, you can't do it.