phpのregister_shutdown_function()で登録するシャットダウン関数と、グローバルスコープ上のオブジェクトの__destruct()の優先順位

デストラクタの方を先に実行してほしかったのですが、どうもシャットダウン関数が先に実行されているようです。
公式ドキュメントのコメントで、簡単な例を提示してくれているのを見つけたので、今後の為にも記録しておきます。


デストラクタを持つオブジェクトがローカル変数にnewされてたり、意図的にunset()すれば、シャットダウン関数より先にデストラクタを実行させる事もできるみたいです。

i have written a quick example about the order of destructors and shutdown functions in php 5.2.1:

<?php
class destruction {
    var $name;

    function destruction($name) {
        $this->name = $name;
        register_shutdown_function(array(&$this, "shutdown"));
    }

    function shutdown() {
        echo 'shutdown: '.$this->name."\n";
    }

    function __destruct() {
        echo 'destruct: '.$this->name."\n";
    }
}

$a = new destruction('a: global 1');

function test() {
    $b = new destruction('b: func 1');
    $c = new destruction('c: func 2');
}
test();

$d = new destruction('d: global 2');

?>

this will output:
shutdown: a: global 1
shutdown: b: func 1
shutdown: c: func 2
shutdown: d: global 2
destruct: b: func 1
destruct: c: func 2
destruct: d: global 2
destruct: a: global 1

conclusions:
destructors are always called on script end.
destructors are called in order of their "context": first functions, then global objects
objects in function context are deleted in order as they are set (older objects first).
objects in global context are deleted in reverse order (older objects last)

shutdown functions are called before the destructors.
shutdown functions are called in there "register" order. ;)
PHP: コンストラクタとデストラクタ - Manual