Home  >  Article  >  Backend Development  >  Analysis of PHP register_shutdown_function function

Analysis of PHP register_shutdown_function function

不言
不言Original
2018-04-09 16:13:531554browse

This article is an analysis and introduction to the PHP register_shutdown_function function. Friends who need it can refer to it

Scripts often die, and they don’t always look good. We don’t want to display a fatal error to the user. Or a blank page (when display_errors is set to off). There is a function called register_shutdown_function in PHP that allows us to set another function that can be called when the execution is shut down.That is It is said that when our script execution is completed or unexpectedly dies, causing PHP execution to be shut down, our function will be called . Therefore, we can set a variable to false at the beginning of the script, and then Setting it to true at the end of the script lets PHP close the callback function to check if the script is complete. If our variable is still false, we know that the last line of the script was not executed, so it must have died somewhere in the execution of the program. . I have prepared a very basic example to demonstrate how you should give the user some appropriate feedback when a fatal error needs to be displayed. You can turn off the display of fatal errors (Annotation: you can set display_errors and error_reporting), so that The example looks better.

Copy code The code is as follows:


$clean = false;
function shutdown_func(){
global $clean;
if (!$clean){
die("not a clean shutdown");
}
return false;
}
register_shutdown_function("shutdown_func");
$a = 1;
$a = new FooClass(); // Will fail with fatal error
$clean = true;
?>


As you can see, if the clean variable is not set to true when the shutdown callback function is running, the shutdown_func function will print something. This thing can be packaged into a class ( Do not use global variables).
PHP provides the register_shutdown_function() function, which can call back the registered function before the script terminates, that is, the function that is executed after the PHP program execution is completed.
register_shutdown_function The execution mechanism is: PHP transfers the function to be called into memory. This function is called again when all PHP statements on the page have been executed. Note that at this time it is called from memory, not from the PHP page, so the above example cannot use relative paths because PHP has already assumed that the original page does not exist. There is no relative path at all.
Note: register_shutdown_function refers to calling the function after all PHP statements have been executed. Do not understand it as calling the function when the client closes the streaming browser page.
You can understand the calling conditions like this:
1. When the page is forced to stop by the user
2. When the program code runs out of time
3. When the PHP code is executed When completed, there are exceptions, errors, and warnings in code execution
Related recommendations:

How to use static variables in PHP functions              

The above is the detailed content of Analysis of PHP register_shutdown_function function. For more information, please follow other related articles on the PHP Chinese website!

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