_ Destruct and register_shutdown_function execution sequence problem, destruct_PHP tutorial-php Tutorial

Source: Internet
Author: User
_ Destruct and register_shutdown_function execution sequence problem, destruct. _ Destruct and register_shutdown_function are executed sequentially. destruct is resolved according to the php Manual. _ Destruct indicates that the destructor will be deleted from all references to an object or the order of execution of _ destruct and register_shutdown_function. destruct

According to the php Manual.

_ Destruct is

   

The Destructor is executed when all references to an object are deleted or when the object is explicitly destroyed.

While register_shutdown_function is

   

RegisterscallbackTo be executed after script execution finishes or exit () is called. registers a callback function that is executed when the script is run or exit () is called.

Literally speaking, __destruct is at the object level, while register_shutdown_function is at the script level. it is supposed that register_shutdown_function has a higher level and the registered function should be executed at the end. To prove our guesses, we have written a script:

register_shutdown_function(function(){echo 'global';});    class A {            public function __construct(){        }                public function __destruct()        {            echo __class__,'::',__function__,'
'; } } new A;

Execution result:

A: :__ destruct
Global

It fully proves our conjecture that it is executed in the order of object-> script.

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

class A {            public function __construct(){            register_shutdown_function(function(){echo 'local', '
';}); } public function __destruct() { echo __class__,'::',__function__,'
'; } } new A;

Result:

Local
A: :__ destruct

We can see that register_shutdown_function is called first, and finally the _ destruct of the execution object. This indicates that the function registered by register_shutdown_function is treated as a method in the class ?! Unknown, this may need to view the php source code for parsing.

We can expand the scope to view the situation:

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 globally, register another function in class AB, and the class also has the destructor. What is the final running result?

Global
B: op
A: op
A: :__ destruct
B: :__ destruct

The result completely subverts our imagination. the register_shutdown_function is first executed no matter whether it is registered in the class or globally, and the execution sequence in the class is the order in which they are registered. If we study the global register_shutdown_function both in front and in the back, it seems that the result is returned, that is, register_shutdown_function is executed first than _ destruct, the global register_shutdown_function is executed before the register_shutdown_function registered in the class.

I cannot accept this result. According to this conclusion, it is hard to say that the script can be executed again after it is completed _ destruct ?! Therefore, I will continue to verify this conclusion-remove the registry register_shutdown_function in the class and keep the global register_shutdown_function:

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:

A: :__ destruct
Global
B: :__ destruct

The result is confusing. the execution sequence of destructor A and B is unquestionable. Because Class A is definitely destroyed before class B when class A is called in Class B, but how can the global register_shutdown_function be executed in the middle of them ?! Confusing.

According to the manual parsing, the destructor can also be executed when exit is called.

  

The destructor will be called even when the exit () termination script is used. Calling exit () in the destructor will stop other close operations.

How can exit be called in a function?

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:

Global
B: op
A: op
B: :__ destruct
A: :__ destruct

This order is similar to the third example above. What is different and incredible is that class B destructor are executed before class, will all references to Class A be destroyed after class B is destroyed ?! Unknown.

 

Conclusion:
  • 1. try not to mix register_shutdown_function and _ destruct in the script. Their behavior is completely unpredictable.
  • 1. because the objects are being referenced by each other, we cannot determine when the objects will be destroyed. when the content needs to be output in order, the content should not be placed in the destructor _ destruct;
  • 2. try not to register register_shutdown_function in the class, because its order is difficult to predict (the function is registered only when this object is called), and _ destruct can replace register_shutdown_function;
  • 3. if you need to execute relevant actions when the script exits, it is best to register register_shutdown_function at the beginning of the script and put all the actions in one function.

Please let us know.


Php paging problems

This is a solution that considers the compatibility between php4 and php5 ..

In php 4, constructor is the same as the class name. In php 5, the _ construct method is used. This causes a compatibility problem. In PHP 4, the constructor is the page method. In php 5, the constructor is the _ construct method.

The two methods in this class exist at the same time, so that they can be used normally under php4 and php5. If this class is run in php 4, the page method is called as the constructor. So that the real constructor can run normally ..

The _ destruct method is the Destructor in php 5. Run the command at the end of code execution ..
Check whether this method exists in the page method. If yes, run this method when the code ends .. The purpose is to be able to use destructor normally in PHP 4 ..

======================================
The explanation of this code has been answered above .. What are the specific functions used. Check the php 5 manual ..


Parse according to the php Manual. _ Destruct indicates that the destructor will be deleted from all references to an object or...

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.