PHP parent caller function name


TL;DR: Get the parent caller function name in PHP with:

debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function']

To get the current function name, use the magic constant __METHOD__ or __FUNCTION__:

function foo() {
   echo __METHOD__;
}

foo(); // 'foo'

To get the parent caller function name, use debug_backtrace():

function parent() {
    child();
}

function child() {
    echo debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function'];
}

child(); // 'parent'

The option DEBUG_BACKTRACE_IGNORE_ARGS and limit 2 stack frames were passed to save memory.



Please support this site and join our Discord!