`debug_backtrace()` reports abstract classes in trace rather than concrete classes. Is there a fix?

61 Views Asked by At

I wrote the following code which turns the result of debug_backtrace() into a string:

function debug_backtrace_string($skip = 1)
{
    try
    {
        $calls = debug_backtrace();
        $calls = array_slice($calls, $skip);
        $str = [];
        foreach ($calls as $call)
        {
            if (isset($call['args']))
                foreach ($call['args'] as $k => $v)
                    $call['args'][$k] = gettype($v).' '.'"'.substr($v, 0, 100).'"';
            else
                $call['args'] = [];
            $str[] = ($call['file']??'')
                    .(isset($call['line'])?' ('.$call['line'].')':'')
                    ." ".($call['class']??'').($call['type']??'').$call['function']
                    .'('.implode(', ', $call['args']).')';
        }
        return implode("\n", $str);
    }
    catch (Throwable $e)
    {
        return "debug_backtrace_string($skip) failed: ".$e;
    }
}

The problem is I'm trying to debug quite a lot of code in an abstract class, and it's extended by 3 classes. But more often than not, the above code spits out the name of the abstract class rather than the concrete class, and it's really making life difficult because I'm having trouble differentiating which concrete class is doing what.

Is there a way round this?

0

There are 0 best solutions below