__destruct and Register_shutdown_function in PHP in order to perform sequencing issues, destruct_php tutorial

Source: Internet
Author: User
Tags exit in php source code

__destruct and Register_shutdown_function in PHP in order to perform the sequencing problem, destruct


Based on the PHP manual parsing.

__destruct is

Destructors are removed when all references to an object are deleted or executed when an object is explicitly destroyed.

and register_shutdown_function is

Registers a callback to being executed after script execution finishes or exit () is called. Registers a callback function that executes when the script has finished running or when it calls exit ().

Literally, __destruct is at the object level, and register_shutdown_function is the entire scripting plane and should be register_shutdown_function higher, and its registered functions should be executed last. To prove our guess, we write a script:

Copy the Code code as follows:
Register_shutdown_function (function () {echo ' Global ';});
Class A {
Public Function __construct () {
}
Public Function __destruct ()
{
Echo __class__, ':: ', __function__, '
';
}
}
New A;

Execution Result:

Copy the Code code as follows:
A::__destruct
Global

It completely confirms our guess that it was executed in the order of the object-and-script.

But what if we registered register_shutdown_function in the object? Is it still in the same order?!

Copy the Code code as follows:
Class A {
Public Function __construct () {
Register_shutdown_function (function () {echo ' local ', '
';});
}
Public Function __destruct ()
{
Echo __class__, ':: ', __function__, '
';
}
}
New A;

Results:

Copy the Code code as follows:
Local
A::__destruct

You can see that register_shutdown_function is called first, and finally the __destruct that executes the object. This indicates that the register_shutdown_function registered function is treated as a method in the class?! Not known, this may need to see the PHP source code to resolve.

We can expand the scope to see the situation:

Copy the Code code as follows:
Register_shutdown_function (function () {echo ' Global ', '
';});
Class A {
Public Function __construct () {
Register_shutdown_function (Array ($this, ' op '));
}
Public Function __destruct ()
{
Echo __class__, ':: ', __function__, '
';
}
Public Function op ()
{
Echo __class__, ':: ', __function__, '
';
}
}
Class B {
Public Function __construct ()
{
Register_shutdown_function (Array ($this, ' op '));
$obj = new A;
}
Public Function __destruct ()
{
Echo __class__, ':: ', __function__, '
';
}
Public Function op ()
{
Echo __class__, ':: ', __function__, '
';
}
}
$b = new B;

We register a register_shutdown_function function globally, and then we register one in class AB, and there are destructors in the class. What happens when you run the final result?

Copy the Code code as follows:
Global
B::op
A::op
A::__destruct
B::__destruct

The result completely overturned our imagination that the Register_shutdown_function function is executed first, regardless of whether it is registered in the class or globally, and the order in which the classes are executed is the order in which they are registered. If we look more closely, the global register_shutdown_function function either in front or behind is the result, things seem to have the result, that is register_shutdown_function than __destruct first execute , the global register_shutdown_function function is executed before the register_shutdown_function registered in the class.

Wait, I can't accept the result, according to this conclusion, it is said that after the script has been finished can be executed __destruct?! Therefore, I will continue to verify this conclusion---Remove the registration register_shutdown_function in the class, and preserve the global register_shutdown_function:

Copy the Code code as follows:
Class A {
Public Function __destruct ()
{
Echo __class__, ':: ', __function__, '
';
}
}
Class B {
Public Function __construct ()
{
$obj = new A;
}
Public Function __destruct ()
{
Echo __class__, ':: ', __function__, '
';
}
}
Register_shutdown_function (function () {echo ' Global ', '
';});

Output:

Copy the Code code as follows:
A::__destruct
Global
B::__destruct

The results are confusing, and the sequence of destructors for A and B two classes is no doubt, because B calls a, Class A is definitely better than B, but how does the global register_shutdown_function function be executed in between?! Convoluted.

By parsing the manual, destructors can also be executed when the exit is called.

Destructors are called even when the script is run with exit (). Calling exit () in the destructor will abort the operation of the rest of the shutdown operation.

If you call exit in a function, how are they called?

Copy the Code code as follows:
Class A {
Public Function __construct () {
Register_shutdown_function (Array ($this, ' op '));
Exit
}
Public Function __destruct ()
{
Echo __class__, ':: ', __function__, '
';
}
Public Function op ()
{
Echo __class__, ':: ', __function__, '
';
}
}
Class B {
Public Function __construct ()
{
Register_shutdown_function (Array ($this, ' op '));
$obj = new A;
}
Public Function __destruct ()
{
Echo __class__, ':: ', __function__, '
';
}
Public Function op ()
{
Echo __class__, ':: ', __function__, '
';
}
}
Register_shutdown_function (function () {echo ' Global ', '
';});
$b = new B;

Output:

Copy the Code code as follows:
Global
B::op
A::op
B::__destruct
A::__destruct

This sequence is similar to the third example above, and it is different and incredible that the Class A destructor is executed before Class A, and all references to Class A after B are destroyed. Unknown.

Conclusion:
1, try not to mix register_shutdown_function and __destruct in scripts, their behavior is completely unpredictable.
1, because objects are referenced in each other, so we can not know when the object is destroyed, when the need to output content sequentially, should not put the content in the destructor __destruct;
2, try not to register register_shutdown_function in the class, because its order is difficult to predict (only when the object is called to register the function), and __destruct can completely replace the register_shutdown_function ;
3, if you need to perform the action when the script exits, it is best to register register_shutdown_function at the beginning of the script and put all the actions in a function.
Please correct us.


Questions for PHP destructor __destruct ()

Destructors are code that is called when an object is destroyed.
When this object is exhausted, the statements in this function are executed automatically.
Put the code for the database shutdown here. is to close the database connection when the object is destroyed.

is the __destruct () destructor in Php an empty method, or does the Shkong be executed?

You can do things like close files, close databases, and so on. such as the Word program is very silly, open the file itself will not close, you run a few times, and then open the Task manager can see, a lot of word in the process, you have to manually switch off.

http://www.bkjia.com/PHPjc/895116.html www.bkjia.com true http://www.bkjia.com/PHPjc/895116.html techarticle PHP __destruct and register_shutdown_function Implementation of the sequencing problem, destruct based on the PHP manual parsing. __destruct is a destructor that will be deleted on all references to an object ...

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