Skip to content

Instantly share code, notes, and snippets.

@vyspiansky
Last active October 23, 2025 10:24
Show Gist options
  • Select an option

  • Save vyspiansky/c3f7f844c003a4adceff0d1da9d7d5fa to your computer and use it in GitHub Desktop.

Select an option

Save vyspiansky/c3f7f844c003a4adceff0d1da9d7d5fa to your computer and use it in GitHub Desktop.
Determine which function called the current function in PHP

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';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment