't Bijstere spoor

't Bijstere spoor

A blog about Web development

PHP shutdown sequence

I needed to figure out in what order PHP shuts down, after the end of a script has been reached, so I created a small testing script. Maybe this is of use for someone else trying to google this like I tried.

<?php

    
// Testing shutdown sequence

    
function shutdown() {

        echo 
"register_shutdown_function\n";

    }

    
register_shutdown_function('shutdown');


    class 
MyClass {

        function 
__destruct() {

           echo 
"Object destructor\n";

        }


    }

    function 
obcallback($buffer) {

        
$buffer .= "Output buffer callback\n";
        return 
$buffer;

    }

    
ob_start('obcallback');

    
$myObject = new MyClass;

    function 
dummy() { }

    function 
sessionclose() {

        echo 
"Session close\n";

    }

    function 
sessionwrite() {

        echo 
"Session write\n";
    }

    
session_set_save_handler('dummy','sessionclose','dummy','sessionwrite','dummy','dummy');

    
session_start();

?>

The output, on PHP 5.2.0 on the cli is:

register_shutdown_function
Object destructor
Output buffer callback
Session write
Session close

I was mostly interested in this because I wanted to work with a custom session handler. This means I can basically not use objects in combination with session handlers, unless I don't rely on $this.


Comments

Richard Thomas
Richard Thomas said on Monday, 5 November 2007 at 6:32 pm CET

Yes you can, you need to register session_close in your register_shutdown_function

Or

register_shutdown_function("session_write_close");

This closes the session up prior to the objects being destroyed

Evert
Evert said on Monday, 5 November 2007 at 6:58 pm CET

Thats smart!

Johannes
Johannes said on Tuesday, 6 November 2007 at 6:36 pm CET

That part of the PHP code is een good commented, so you can et the real order without much internals&C knowledge: http://lxr.php.net/source/php-src/main/main.c#1546

Evert
Evert said on Tuesday, 6 November 2007 at 7:12 pm CET

That is pretty cool.. looks like I haven't really missed anything =)

Adam
Adam said on Tuesday, 13 November 2007 at 12:18 am CET

I always love investigative PHPing. Good work and very useful!

open source
open source said on Saturday, 22 December 2007 at 6:13 pm CET

I think it is a tip of the day :)

zib3r
zib3r said on Monday, 19 May 2008 at 11:34 am CEST

Well , man .... it was awesome.. of course. You can be better than that.But what can i say.You can say just that you're the best and fuck the rest.Keep it up the good work.Bye . From Oradea/Romania/Alexandru.







Solve this simple math problem to prevent bots from spamming this blog:
5 + 7 =