In depth analysis of the PHP register_shutdown_function function
- 2020-06-07 04:03:30
- OfStack
Scripts often die, and not always so good. We don't want to show the user a fatal error, or a blank page (in the case of display_errors set as off). Have a function called register_shutdown_function PHP, allows us to set one when performing the shut down another one function can be invoked. That is to say when we finish in executing the script or accident die PHP perform the closing, we this function will be Been called. So, we can use the start set a variable in the script for false, then set it at the end of the script for true method, let PHP closed callback function check script to complete or not. If our variable is still false, we know the last 1 script lines are not performed, so it must be somewhere in the program execution to died. I prepared a very basic example, can be demonstrated in one How do you give the user proper feedback when fatal errors need to be displayed? You can make the example look better by turning off the fatal errors display.
As you can see, if the clean variable is not set to true when the callback function is closed, the shutdown_func function will print out something that can be wrapped as a class (without using global variables).
PHP provides the function register_shutdown_function() that can call back registered functions before the script terminates, that is, functions that are executed when the PHP program completes execution.
The register_shutdown_function execution mechanism is: PHP calls the function into memory. This function is called when all the PHP statements on the page have been executed. Note that the call is made from memory at this point, not from a PHP page, so the above example cannot use a relative path because PHP is no longer present as the original page. There is no relative path.
Note: register_shutdown_function refers to calling the function after all PHP statements have been executed, not when the client closes the streaming browser page.
The call condition can be understood as follows:
1. When the page is forced to stop by the user
2. When the program code runs timeout
3. When the PHP code execution is completed, there are exceptions, errors and warnings in the code execution
<?php
$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 because of a fatal error
$clean = true;
?>
As you can see, if the clean variable is not set to true when the callback function is closed, the shutdown_func function will print out something that can be wrapped as a class (without using global variables).
PHP provides the function register_shutdown_function() that can call back registered functions before the script terminates, that is, functions that are executed when the PHP program completes execution.
The register_shutdown_function execution mechanism is: PHP calls the function into memory. This function is called when all the PHP statements on the page have been executed. Note that the call is made from memory at this point, not from a PHP page, so the above example cannot use a relative path because PHP is no longer present as the original page. There is no relative path.
Note: register_shutdown_function refers to calling the function after all PHP statements have been executed, not when the client closes the streaming browser page.
The call condition can be understood as follows:
1. When the page is forced to stop by the user
2. When the program code runs timeout
3. When the PHP code execution is completed, there are exceptions, errors and warnings in the code execution