当我使用getTraceAsString()
方法查看异常跟踪时,我始终获得额外的堆栈帧。它在getTrace()
和debug_backtrace()
中都不可见。它来自哪里,它意味着什么?
例如,这个脚本:
function foo () {
echo "== debug_backtrace() ==\n";
foreach (debug_backtrace() as $i => $frame) {
echo "#$i $frame[file]($frame[line]): $frame[function]()\n";
}
throw new Exception();
}
function bar () {
foo();
}
try {
bar();
} catch (Exception $e) {
echo "\n== getTrace() ==\n";
foreach ($e->getTrace() as $i => $frame) {
echo "#$i $frame[file]($frame[line]): $frame[function]()\n";
}
echo "\n== getTraceAsString() ==\n";
echo $e->getTraceAsString(), "\n";
}
产生以下输出:
== debug_backtrace() ==
#0 /home/foo/tests/php/test4.php(13): foo()
#1 /home/foo/tests/php/test4.php(17): bar()
== getTrace() ==
#0 /home/foo/tests/php/test4.php(13): foo()
#1 /home/foo/tests/php/test4.php(17): bar()
== getTraceAsString() ==
#0 /home/foo/tests/php/test4.php(13): foo()
#1 /home/foo/tests/php/test4.php(17): bar()
#2 {main}
我见过{main}的另一个地方是Xdebug。我猜它是一些内部PHP堆栈框架,但为什么只有在将跟踪作为字符串时才可见?