To do this we can use debug_backtrace:
// $backtrace = debug_backtrace();
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
// $caller = isset($backtrace[1]['function']) ? $backtrace[1]['function'] : 'unknown';
$caller = $backtrace[1]['function'] ?? 'unknown';The caller is at index 1 (index 0 is the current function).
If we only need the function name, use debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2) to limit the backtrace depth and ignore arguments for better performance.
If called from a class method, then we can also find out the class
$class = $backtrace[1]['class'] ?? 'unknown';