How does the debug_backtrace function in PHP help in retrieving information about the calling class or method?

The debug_backtrace function in PHP helps in retrieving information about the calling class or method by providing a stack trace of function calls leading up to the current point in the code. This can be useful for debugging purposes or for understanding the flow of execution in a program.

function getCallingClassOrMethod() {
    $trace = debug_backtrace();
    $caller = $trace[1];
    
    if(isset($caller['class'])) {
        return $caller['class'];
    } elseif(isset($caller['function'])) {
        return $caller['function'];
    }
    
    return null;
}

// Example usage
$callingEntity = getCallingClassOrMethod();
echo "Calling entity: " . $callingEntity;