PHP register_shutdown_function () function example, shutdown Function

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.