如何判断该函数是否是从输出缓冲回调中调用的?

发布于 2024-12-19 14:05:31 字数 377 浏览 4 评论 0原文

在函数中,如何确定它是否是从输出缓冲回调中调用的(不一定是直接调用的)?

function foo() {
    if (magic here ????)
        $log->write("foo:Callback") 
    else
        $log->write("foo:Normal")
}

function calls_foo() {
    ...stuff
    foo();
}

calls_foo() // should log foo:Normal

ob_start('calls_foo')

    ...stuff

// should log foo:Callback at the end of the script

In a function, how can I find out if it's been called from an output buffering callback (not necessarily directly)?

function foo() {
    if (magic here ????)
        $log->write("foo:Callback") 
    else
        $log->write("foo:Normal")
}

function calls_foo() {
    ...stuff
    foo();
}

calls_foo() // should log foo:Normal

ob_start('calls_foo')

    ...stuff

// should log foo:Callback at the end of the script

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晨光如昨 2024-12-26 14:05:31

检查$debug = debug_backtrace()。如果调试数组的长度为 1,那么您会从 main 调用,这意味着这是在请求结束时调用的 ob 回调。然后,您可以迭代该数组并查看每个数组的 'function' 键以获取 ob 刷新函数。

Check $debug = debug_backtrace(). If the debug array is 1 long then you get called from main which means this is an ob callback called at the end of the request. Then you can iterate the array and look at the 'function' key of each array for an ob flushing function.

a√萤火虫的光℡ 2024-12-26 14:05:31

函数 ob_get_status() 返回一个包含以下信息的数组输出缓冲的当前状态。

The function ob_get_status() returns an array containing information on the current status of output buffering.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文