Example of using the PHPregister_shutdown_function () function: shutdown function. Example of using the PHPregister_shutdown_function () function. the shutdown function uses the register_shutdown_function method to set another PHP register_shutdown_function () function that can be called when execution is disabled. shutdown function
Through the register_shutdown_function method, we can set another function that can be called when the function is disabled.
That is to say, when the execution of our script is completed or the execution of php is about to close due to unexpected death, our function will be called.
[Application scenario]
① The page is forcibly stopped by (user)
② Unexpected termination or timeout of program code
③ Php4 has no destructor. you can use this function to simulate the destructor.
Shutdown. php
The code is as follows:
<? Php
Header ("content-type: text/html; charset = utf-8 ");
Class Shutdown {
Public function endScript (){
If (error_get_last ()){
Echo'
';
print_r(error_get_last());
echo '
';
}
File_put_contents ('d: \ practice \ php \ Error \ error.txt ', 'This is a test ');
Die ('script termination ');
}
}
Register_shutdown_function (array (new Shutdown (), 'endscript '));
// Error test
Echo md6 ();
Execution, output:
The code is as follows:
(! ) Fatal error: Call to undefined function md6 () in D: \ practice \ php \ Error \ shutdown. php on line 18
Array
(
[Type] => 1
[Message] => Call to undefined function md6 ()
[File] => D: \ practice \ php \ Error \ shutdown. php
[Line] => 18
)
Script ended
The code is as follows:
D: \ practice \ php \ Error \ error.txt:
This is a test
Note: The register_shutdown_function method is called from the memory. Therefore, when using the file_put_contents method, the first parameter must use an absolute path.
Example of using the http://www.bkjia.com/PHPjc/1021089.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1021089.htmlTechArticlePHP register_shutdown_function () function, the shutdown function through the register_shutdown_function method, we can set another one that can be called when the execution is closed...