This article mainly introduces examples of using the PHP register_shutdown_function() function. When our script completes execution or unexpectedly dies, causing PHP to When the execution is about to be shut down, the register_shutdown_function() function will be called. Friends in need can refer to it
The register_shutdown_function method allows us to set another function that can be called when execution is shut down.
That is to say, our function will be called when our script execution is completed or unexpected death causes php execution to be shut down.
【Usage Scenario】
① The page was forced to stop by (user)
② The program code terminates unexpectedly or times out
③ There is no destructor in php4, you can use this function to simulate the destructor
shutdown.php
The code is as follows:
Header("content-type:text/html;charset=utf-8");
class Shutdown{
public function endScript(){
if(error_get_last()){
echo '
';<p> </p> <p> print_r(error_get_last());</p> <p> echo '</p>
';
}
file_put_contents('D:practisephpErrorerror.txt', 'this is a test');
die('script ends');
}
}
register_shutdown_function(array(new Shutdown(), 'endScript'));
//Error testing
echo md6();
Execution, output:
The code is as follows:
( ! ) Fatal error: Call to undefined function md6() in D:practisephpErrorshutdown.php on line 18
Array
(
[type] => 1
[message] => Call to undefined function md6()
[file] => D:practisephpErrorshutdown.php
[line] => 18
)
End of script
The code is as follows:
D:practisephpErrorerror.txt:
this is a test
Note: The register_shutdown_function method is called from memory, so when using the file_put_contents method, the first parameter must use an absolute path.