search
HomeBackend DevelopmentPHP TutorialThe order of execution of __destruct and register_shutdown_function, destruct_PHP tutorial

__destruct and register_shutdown_function execution order issue, destruct

is based on the analysis of the PHP manual.

__destruct is

   <p>析构函数会在到某个对象的所有引用都被删除或者当对象被显式销毁时执行。</p> 

and register_shutdown_function is

   <p>Registers a <code>callback</code> to be executed after script execution finishes or exit() is called. 注册一个回调函数,此函数在脚本运行完毕或调用exit()时执行。</p> 

Literally understood, __destruct is at the object level, and register_shutdown_function is at the entire script level, so register_shutdown_function should be is a higher level, the functions registered by it should also be executed last. To confirm our guess, we write a script:

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

Execution result:

A::__destruct
global

Completely confirmed our guess, it was executed in the order of object->script.

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

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

Result:

local
A::__destruct

You 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? ! I don't know, this may require viewing the php source code to parse it.

We can expand the scope to see the situation:

<span>register_shutdown_function</span>(<span>function</span>(){<span>echo</span> 'global', '<br/>'<span>;});
    </span><span>class</span><span> A {
    
        </span><span>public</span> <span>function</span><span> __construct(){
            </span><span>register_shutdown_function</span>(<span>array</span>(<span>$this</span>, 'op'<span>));</span>
<span></span><span>        }
        
        </span><span>public</span> <span>function</span><span> __destruct()
        {
            </span><span>echo</span> <span>__class__</span>,'::',<span>__function__</span>,'<br/>'<span>;
        }
        
        </span><span>public</span> <span>function</span><span> op()
        {
            </span><span>echo</span> <span>__class__</span>,'::',<span>__function__</span>,'<br/>'<span>;
        }
        
    }

    </span><span>class</span><span> B {
    
        </span><span>public</span> <span>function</span><span> __construct()
        {
            </span><span>register_shutdown_function</span>(<span>array</span>(<span>$this</span>, 'op'<span>));
            </span><span>$obj</span> = <span>new</span><span> A;
        }
        
        </span><span>public</span> <span>function</span><span> __destruct()
        {
            </span><span>echo</span> <span>__class__</span>,'::',<span>__function__</span>,'<br/>'<span>;
        }
        
        </span><span>public</span> <span>function</span><span> op()
        {
            </span><span>echo</span> <span>__class__</span>,'::',<span>__function__</span>,'<br/>'<span>;
        }
    
    }
    
    </span><span>$b</span> = <span>new</span> B;

We register a register_shutdown_function function globally, and register another one in class AB, and there are destructor methods in each class. What will be the final result?

global
B::op
A::op
A::__destruct
B::__destruct

The result completely subverted our imagination. No matter whether the register_shutdown_function function is registered in the class or globally, it is executed first. The order of execution in the class is the order in which they are registered. If we study carefully, the global register_shutdown_function function will have this result whether it is placed in front or behind. Things seem to have a result, that is, register_shutdown_function is executed before __destruct, and the global register_shutdown_function function is executed before the register_shutdown_function registered in the class. .

Wait a minute, I cannot accept this result. Based on this conclusion, does it mean that __destruct can be executed after the script has ended? ! Therefore, I will continue to verify this conclusion---remove the register_shutdown_function in the class and retain the global register_shutdown_function:

<span>class</span><span> A {
        
        </span><span>public</span> <span>function</span><span> __destruct()
        {
            </span><span>echo</span> <span>__class__</span>,'::',<span>__function__</span>,'<br/>'<span>;
        }
</span><span> 
    }

    </span><span>class</span><span> B {
    
        </span><span>public</span> <span>function</span><span> __construct()
        {
            </span><span>$obj</span> = <span>new</span><span> A;
        }
        
        </span><span>public</span> <span>function</span><span> __destruct()
        {
            </span><span>echo</span> <span>__class__</span>,'::',<span>__function__</span>,'<br/>'<span>;
        }
</span><span>   
    }
    
    </span><span>register_shutdown_function</span>(<span>function</span>(){<span>echo</span> 'global', '<br/>';});

Output:

A::__destruct
global
B::__destruct

The results are confusing. There is no doubt about the execution order of the destructors of classes A and B. Because A is called in B, class A must be destroyed before B, but how can the global register_shutdown_function function be sandwiched between them? be executed? ! Puzzling.

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

  <p>析构函数即使在使用 exit()终止脚本运行时也会被调用。在析构函数中调用 exit() 将会中止其余关闭操作的运行。</p>

If exit is called in a function, how are they called?

<span>class</span><span> A {
    
        </span><span>public</span> <span>function</span><span> __construct(){
            </span><span>register_shutdown_function</span>(<span>array</span>(<span>$this</span>, 'op'<span>));
            </span><span>exit</span><span>;
        }
        
        </span><span>public</span> <span>function</span><span> __destruct()
        {
            </span><span>echo</span> <span>__class__</span>,'::',<span>__function__</span>,'<br/>'<span>;
        }
        
        </span><span>public</span> <span>function</span><span> op()
        {
            </span><span>echo</span> <span>__class__</span>,'::',<span>__function__</span>,'<br/>'<span>;
        }
        
    }

    </span><span>class</span><span> B {
    
        </span><span>public</span> <span>function</span><span> __construct()
        {
            </span><span>register_shutdown_function</span>(<span>array</span>(<span>$this</span>, 'op'<span>));
            </span><span>$obj</span> = <span>new</span><span> A;
        }
        
        </span><span>public</span> <span>function</span><span> __destruct()
        {
            </span><span>echo</span> <span>__class__</span>,'::',<span>__function__</span>,'<br/>'<span>;
        }
        
        </span><span>public</span> <span>function</span><span> op()
        {
            </span><span>echo</span> <span>__class__</span>,'::',<span>__function__</span>,'<br/>'<span>;
        }
    
    }
    
    </span><span>register_shutdown_function</span>(<span>function</span>(){<span>echo</span> 'global', '<br/>'<span>;});
    
    </span><span>$b</span> = <span>new</span> B;

Output:

global
B::op
A::op
B::__destruct
A::__destruct

This sequence is similar to the third example above. What is different and incredible is that the destructor of class B is executed before class A. Is it true that all references to class A are destroyed only after B is destroyed? ! unknown.

Conclusion:

  • 1. Try not to mix register_shutdown_function and __destruct in scripts. Their behavior is completely unpredictable.
  • 1. Because objects refer to each other, we cannot know when the object 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 will only be registered when this object is called), and __destruct can completely replace register_shutdown_function;
  • 3. If you need to perform relevant actions when the script exits, it is best to register register_shutdown_function at the beginning of the script and put all actions in a function.

Please correct me.

php paging problem

This is a solution taking into account php4 and php5 compatibility issues. .

In the PHP 4 version, the constructor is a method with the same name as the class. In PHP 5, it is the __construct method. This creates a compatibility issue. Under PHP 4, the constructor is the page method. In the PHP 5 version, the constructor is the __construct method.

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

__destruct method is the destructor in PHP 5. Executed at the end of the code run. .
Check whether this method exists in the page method. If it exists, run this method when the code ends. . The purpose is also to be able to use the destructor normally under PHP 4. .

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/894762.htmlTechArticleThe order of execution of __destruct and register_shutdown_function, destruct is analyzed according to the PHP manual. __destruct is a destructor that occurs when all references to an object are deleted or...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How do you modify data stored in a PHP session?How do you modify data stored in a PHP session?Apr 27, 2025 am 12:23 AM

TomodifydatainaPHPsession,startthesessionwithsession_start(),thenuse$_SESSIONtoset,modify,orremovevariables.1)Startthesession.2)Setormodifysessionvariablesusing$_SESSION.3)Removevariableswithunset().4)Clearallvariableswithsession_unset().5)Destroythe

Give an example of storing an array in a PHP session.Give an example of storing an array in a PHP session.Apr 27, 2025 am 12:20 AM

Arrays can be stored in PHP sessions. 1. Start the session and use session_start(). 2. Create an array and store it in $_SESSION. 3. Retrieve the array through $_SESSION. 4. Optimize session data to improve performance.

How does garbage collection work for PHP sessions?How does garbage collection work for PHP sessions?Apr 27, 2025 am 12:19 AM

PHP session garbage collection is triggered through a probability mechanism to clean up expired session data. 1) Set the trigger probability and session life cycle in the configuration file; 2) You can use cron tasks to optimize high-load applications; 3) You need to balance the garbage collection frequency and performance to avoid data loss.

How can you trace session activity in PHP?How can you trace session activity in PHP?Apr 27, 2025 am 12:10 AM

Tracking user session activities in PHP is implemented through session management. 1) Use session_start() to start the session. 2) Store and access data through the $_SESSION array. 3) Call session_destroy() to end the session. Session tracking is used for user behavior analysis, security monitoring, and performance optimization.

How can you use a database to store PHP session data?How can you use a database to store PHP session data?Apr 27, 2025 am 12:02 AM

Using databases to store PHP session data can improve performance and scalability. 1) Configure MySQL to store session data: Set up the session processor in php.ini or PHP code. 2) Implement custom session processor: define open, close, read, write and other functions to interact with the database. 3) Optimization and best practices: Use indexing, caching, data compression and distributed storage to improve performance.

Explain the concept of a PHP session in simple terms.Explain the concept of a PHP session in simple terms.Apr 26, 2025 am 12:09 AM

PHPsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIDstoredinacookie.Here'showtomanagethemeffectively:1)Startasessionwithsession_start()andstoredatain$_SESSION.2)RegeneratethesessionIDafterloginwithsession_regenerate_id(true)topreventsessi

How do you loop through all the values stored in a PHP session?How do you loop through all the values stored in a PHP session?Apr 26, 2025 am 12:06 AM

In PHP, iterating through session data can be achieved through the following steps: 1. Start the session using session_start(). 2. Iterate through foreach loop through all key-value pairs in the $_SESSION array. 3. When processing complex data structures, use is_array() or is_object() functions and use print_r() to output detailed information. 4. When optimizing traversal, paging can be used to avoid processing large amounts of data at one time. This will help you manage and use PHP session data more efficiently in your actual project.

Explain how to use sessions for user authentication.Explain how to use sessions for user authentication.Apr 26, 2025 am 12:04 AM

The session realizes user authentication through the server-side state management mechanism. 1) Session creation and generation of unique IDs, 2) IDs are passed through cookies, 3) Server stores and accesses session data through IDs, 4) User authentication and status management are realized, improving application security and user experience.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.