__destruct and register_shutdown_function implementation of sequencing issues, destruct_php tutorials

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

__destruct and register_shutdown_function implementation of the order of the 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


callbackTo 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:

register_shutdown_function (function() {echo ' Global ';});     class A {            publicfunction  __construct () {        }                public  function  __destruct ()        {            echo__class__, ':: ',__function__ ,'
'; } } new A;

Execution Result:


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?!

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



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:

register_shutdown_function (function() {echo ' global ', '
';}); class A { publicfunction __construct () { register_shutdown_function (array($this, ' op '));
        }                 Public function__destruct () {Echo __class__,'::',__function__,'
'; } Public functionop () {Echo __class__,'::',__function__,'
'; } } classB { Public function__construct () {register_shutdown_function(Array($this, ' op ')); $obj=NewA; } Public function__destruct () {Echo __class__,'::',__function__,'
'; } Public functionop () {Echo __class__,'::',__function__,'
'; } } $b=NewB

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?


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:

class A {                publicfunction  __destruct ()        {            echo__class__ , ':: ',__function__, '
'; } } class B { publicfunction __ Construct () { $objnew A; } Public function __destruct () { echo__class__, ':: ',__function__, '
'; } } register_shutdown_function(function() {echo ' global ', '



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?

classA { Public function__construct () {register_shutdown_function(Array($this, ' op ')); Exit; }                 Public function__destruct () {Echo __class__,'::',__function__,'
'; } Public functionop () {Echo __class__,'::',__function__,'
'; } } classB { Public function__construct () {register_shutdown_function(Array($this, ' op ')); $obj=NewA; } Public function__destruct () {Echo __class__,'::',__function__,'
'; } Public functionop () {Echo __class__,'::',__function__,'
'; } } register_shutdown_function(function(){Echo' Global ', '
';}); $b=NewB



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.


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

Problems with PHP class paging

This is a solution that takes into account the compatibility issues of PHP4 and PHP5.

In PHP 4, constructors are the same method as the class name. and PHP 5 is the __construct method. This creates a compatibility issue. In the PHP 4 version, the constructor is the page method. In the PHP 5 version, the constructor is the __construct method.

There are two methods in this class that can be used normally in PHP4 and PHP5. If you run this class under PHP 4, the page method is a constructor, and the __construct is transferred. Allows the true constructor to function correctly.

The __destruct method is a destructor in PHP 5. Execute at the end of the code run:
The page method checks if this method exists, and if it exists, run this method at the end of the code: The purpose is also to be able to use destructors normally under PHP 4.

The explanation of this code has been answered above. What do you do with the specific functions? Please check the PHP 5 manual.

http://www.bkjia.com/PHPjc/894762.html www.bkjia.com true http://www.bkjia.com/PHPjc/894762.html techarticle __destruct and register_shutdown_function The order of execution, destruct according to the PHP manual parsing. __destruct is a destructor that will be removed from all references to an object or ...

  • Related Article

    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.