【问题标题】:is it possible check if given function is executing within other function or method?是否可以检查给定函数是否在其他函数或方法中执行?
【发布时间】:2011-07-03 17:57:12
【问题描述】:
function echo_parent_func() {
    echo // so what now ?
}

function somefunc() {
    echo_parent_func();
}

somefunc(); //should echo string 'somefunc'

用 php 也可以吗?

【问题讨论】:

标签: php function php-5.3


【解决方案1】:
function get_caller_method() 
{ 
    $traces = debug_backtrace(); 

    if (isset($traces[2])) 
    { 
        return $traces[2]['function']; 
    } 

    return null; 
} 

function echo_parent_func() {
    echo get_caller_method();
}

function somefunc() {
    echo_parent_func();
}

somefunc(); //should echo string 'somefunc'

Source

编辑刚刚也找到this answer

【讨论】:

  • 使用false 作为参数调用debug_backtrace(对于PHP 5.3),这样会更快。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-17
相关资源
最近更新 更多