Home >Backend Development >PHP Tutorial >Example of using the register_shutdown_function function to intercept fatal errors in PHP, fatalerror_PHP tutorial

Example of using the register_shutdown_function function to intercept fatal errors in PHP, fatalerror_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:56:23789browse

Example of using the register_shutdown_function function in PHP to intercept fatal errors, fatalerror

When we are working on projects, fatal errors occasionally occur due to carelessness. If display_errors is set to off, the user will see a blank page. If it is set to on, the fatal error message will be displayed (of course, normal people will not do this).

So is there any way we can intercept fatal errors in advance and feedback them to users in our own custom friendly form? There is a function in PHP called register_shutdown_function that allows us to set another function that can be called when the execution is shut down. That is to say, when our script execution is completed or unexpectedly dies and the PHP execution is about to shut down, this function will is called.
Please see an example below:
Copy code The code is as follows:
$flag = false;
function deal_error(){
global $flag;
If (!$flag){
                                                                                                                                                                                                          ) }
Return false;
}
register_shutdown_function("deal_error");
//Will fail with fatal error
//$obj = new NotExistClass(); //Introducing undefined classes
require('./test.php');
$flag = true;

At the program entry, we set the flag to false, and finally set it to true, indicating that the program is executing normally. If the flag is not true at the end, it means it died somewhere in the middle. At this time, register_shutdown_function will be called to output our customized error result.

If the above class is not defined, non-existent files are introduced (require or require_once must be used), etc., it will lead to fatal errors. Of course, if your program is missing a punctuation mark or has an extra special character or something, then there is nothing you can do.

http://www.bkjia.com/PHPjc/987895.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/987895.htmlTechArticleUsing the register_shutdown_function function in PHP to intercept fatal error examples, fatalerror When we are working on projects, we occasionally appear due to carelessness. fatal error. If display_errors is set to...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn