With the Register_shutdown_function method, let's set up a second function that can be invoked when execution is closed.
That is, our function is invoked when our script executes or dies unexpectedly, causing PHP execution to close.
"Use Scene"
① page is forcibly stopped by (user)
② program code terminated unexpectedly or timed out
There is no destructor in the ③PHP4, you can use this function to simulate a destructor
shutdown.php
Copy Code code 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:\practise\php\Error\error.txt ', ' This is a test ');
Die (' Script End ');
}
}
Register_shutdown_function (Array (new shutdown (), ' endscript '));
Error test
Echo Md6 ();
Execute, Output:
Copy Code code as follows:
( ! ) Fatal error:call to undefined function md6 () into D:\practise\php\Error\shutdown.php on line 18
Array
(
[Type] => 1
[Message] => call to undefined function md6 ()
[File] => D:\practise\php\Error\shutdown.php
[Line] => 18
)
End of script
Copy Code code as follows:
D:\practise\php\Error\error.txt:
This is a test
Note: The Register_shutdown_function method is called from memory, so the first argument must use an absolute path when using the File_put_contents method.