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

Example of using PHP register_shutdown_function() function_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-13 09:49:21
Original
787 people have browsed it

Examples of using the PHP register_shutdown_function() function

This article mainly introduces examples of using the PHP register_shutdown_function() function. When our script completes execution or unexpectedly dies, causing PHP to When the execution is about to be shut down, the register_shutdown_function() function will be called. Friends in need can refer to it

The register_shutdown_function method allows us to set another function that can be called when execution is shut down.

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

【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

The code is as follows:

Header("content-type:text/html;charset=utf-8");

class Shutdown{

public function endScript(){

 if(error_get_last()){

echo '

';<p> </p>
<p>  print_r(error_get_last());</p>
<p>  echo '</p>
Copy after login

';

 }

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:

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

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/1020273.htmlTechArticleUsage examples of the PHP register_shutdown_function() function. This article mainly introduces the use examples of the PHP register_shutdown_function() function. When Our script completes execution or dies unexpectedly...
Related labels:
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template