检测PHP是否正在关闭

时间:2016-11-28 20:33:19

标签: php

如何检测PHP是否正在关闭?通过"关闭",我的意思是用register_shutdown_function注册的函数当前正在执行。例如,请考虑以下代码:

<?php

function do_something() {
    // How can I detect whether PHP is shutting down.
}

register_shutdown_function('do_something');
do_something();

在上面的代码片段中,我希望能够从do_something函数中检测到关闭。

1 个答案:

答案 0 :(得分:0)

这应该有效:

<?php

// receive true or false for knowing if register_shutdown_function() called this function
function do_something($is_register_shutdown_function = false) {
    // How can I detect whether PHP is shutting down.
    if($is_register_shutdown_function === true){
        file_put_contents('do_something.log', "do_something() - ".time().".".microtime(true)."\n", FILE_APPEND);
    }

    // do as you please going forward
}

register_shutdown_function('do_something', true); // pass true to the first parameter of do_something()
do_something();