Example of using the PHPregister_shutdown_function () function. Use example of PHPregister_shutdown_function () function this article mainly introduces the use example of PHPregister_shutdown_function () function. use example of PHP register_shutdown_function () function when the script execution is completed or accidentally killed
This article mainly introduces the example of using the PHP register_shutdown_function () function. when the execution of our script is completed or the execution of php is about to close due to accidental death, the register_shutdown_function () function will be called, for more information, see
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:
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.
Http://www.bkjia.com/PHPjc/1020273.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1020273.htmlTechArticlePHP register_shutdown_function () function examples this article mainly introduces the use of PHP register_shutdown_function () function examples, when our script execution is completed or accidentally killed...