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.


    // Testing shutdown sequence

    function shutdown() {

        echo "register_shutdown_function\n";



    class MyClass {

        function __destruct() {

           echo "Object destructor\n";



    function obcallback($buffer) {

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



    $myObject = new MyClass;

    function dummy() { }

    function sessionclose() {

        echo "Session close\n";


    function sessionwrite() {

        echo "Session write\n";




The output, on PHP 5.2.0 on the cli is:

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.