I think you're looking for debug_print_backtrace.
From the docs:
debug_print_backtrace() prints a PHP backtrace. It prints the function calls,
included/required files and eval()ed stuff.
If, however, you want the data not printed immediately but returned you could use debug_backtrace(), save it to a var and only use the ['function'] index.
Of course this function also is better described in its documentation.
If you want the stack trace after you caught an exception via try catch for example, you could also use this:
try {
// stuff
} catch(\Exception $e){
$stackTrace = $e->getTraceAsString();
// do whatever you want to with your stack trace
}
You can also just create a new Exception using
$e = new \Exception;
And get the stack trace via
$e->getTraceAsString();
That function is also explained further in the documentation.