PHP register_shutdown_function () function example, 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
Copy codeThe Code is as follows:
<? Php
Header ("content-type: text/html; charset = UTF-8 ");
Class Shutdown {
Public function endScript (){
If (error_get_last ()){
Echo '<pre> ';
Print_r (error_get_last ());
Echo '</pre> ';
}
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:
Copy codeThe 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
Copy codeThe 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.