开发者

is it possible check if given function is executing within other function or method?

开发者 https://www.devze.com 2023-03-17 10:37 出处:网络
function echo_parent_func() { echo // so what now ? } function somefunc() { echo_parent_func(); } 开发者_JAVA百科
function echo_parent_func() {
    echo // so what now ?
}

function somefunc() {
    echo_parent_func();
}
开发者_JAVA百科
somefunc(); //should echo string 'somefunc'

Is that even possible with php ?


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

EDIT Just found this answer too:

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号