【问题标题】:Detecting whether PHP is shutting down检测 PHP 是否正在关闭
【发布时间】:2016-11-29 05:05:34
【问题描述】:

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

<?php

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

register_shutdown_function('do_something');
do_something();

在上面的代码 sn-p 中,我希望能够从 do_something 函数中检测关机。

【问题讨论】:

  • “我想检测[...]”是什么意思?如果do_something 被触发,则意味着 PHP 已经关闭
  • 正如@Max 所说,register_shutdown_function() 用于在脚本完成或退出后调用特定函数。脚本完成后将调用您的 do_something() 函数。
  • 啊抱歉,看来我把代码块塞满了。让我解决它。

标签: php


【解决方案1】:

这应该可行:

<?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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-11
    • 2012-01-02
    • 1970-01-01
    • 2011-03-07
    相关资源
    最近更新 更多