Example of using the PHP register_shutdown_function () function _php instance

Source: Internet
Author: User

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.

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.