Sometimes, we want to get the name of the caller function in PHP.
In this article, we’ll look at how to get the name of the caller function in PHP.
How to get the name of the caller function in PHP?
To get the name of the caller function in PHP, we can use the debug_backtrace
function.
For instance, we write
$trace = debug_backtrace();
$caller = $trace[1];
echo "Called by {$caller["function"]}";
if (isset($caller["class"])) {
echo " in {$caller["class"]}";
}
to call debug_backtrace
and get the 2nd entry to get the $caller
array.
We then use $caller["function"]
to get the caller function name and the $caller["class"]
get the caller class name.
Conclusion
To get the name of the caller function in PHP, we can use the debug_backtrace
function.