Home > Backend Development > PHP Tutorial > Example of using PHP register_shutdown_function() function, shutdown function_PHP tutorial

Example of using PHP register_shutdown_function() function, shutdown function_PHP tutorial

WBOY
Release: 2016-07-13 09:48:47
Original
840 people have browsed it

Usage example of PHP register_shutdown_function() function, shutdown function

Through the register_shutdown_function method, we can set another function that can be called when the execution is shut down.

That is to say, our function will be called when our script execution is completed or when the php execution is about to be shut down due to unexpected death.

【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
Copy code The code is as follows:
header("content-type:text/html;charset=utf-8");
class Shutdown{
Public function endScript(){
If(error_get_last()){
echo '

';<br>
                           print_r(error_get_last());<br>
echo '
';
}
           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:

Copy code 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

Copy code 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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1021089.htmlTechArticleAn example of using the PHP register_shutdown_function() function. The shutdown function allows us to set a time when execution is shut down through the register_shutdown_function method. Another one that can be called...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template