Example of using PHP register_shutdown_function () function, shutdown function _php Tutorial

Source: Internet
Author: User

Example of using PHP register_shutdown_function () function, shutdown function


With the Register_shutdown_function method, we can set up another function that can be called when execution is closed.

That is, our function is called when our script executes or the accidental death causes PHP execution to close.

"Use Scene"

① page is forced to stop (user)

② program code terminated unexpectedly or timed out

There is no destructor in ③PHP4, you can use this function to simulate destructors

shutdown.php
Copy the Code code as follows:
<?php
Header ("Content-type:text/html;charset=utf-8");
Class shutdown{
Public Function Endscript () {
if (Error_get_last ()) {
Echo '

';
Print_r (Error_get_last ());
Echo '
';
}
File_put_contents (' D:\practise\php\Error\error.txt ', ' This is a test ');
Die (' End of script ');
}
}

Register_shutdown_function (Array (new shutdown (), ' endscript '));

Error test
Echo Md6 ();

Execute, Output:

Copy the Code code as follows:
( ! ) Fatal error:call to undefined function md6 () inch 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 the 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 parameter must use an absolute path when using the File_put_contents method.

http://www.bkjia.com/PHPjc/1021089.html www.bkjia.com true http://www.bkjia.com/PHPjc/1021089.html techarticle using the PHP register_shutdown_function () Function example, the shutdown function through the Register_shutdown_function method allows us to set another one that can be called when execution is closed ...

  • 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.