3

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 堆栈框架,但为什么它仅在将跟踪作为字符串获取时才可见?

4

0 回答 0