为什么debug_backtrace()有时不包括行号? [英] Why is debug_backtrace() not including line number sometimes?

查看:133
本文介绍了为什么debug_backtrace()有时不包括行号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现有时候 debug_backtrace()不包括呼叫的行号。有什么理由为什么这是和任何方式来纠正它?

I'm finding that sometimes debug_backtrace() is not including the line number for a call. Is there some reason why this is and any way to correct for it?

提前感谢。

是的,它省略行号的电话是我自己的代码,而不是内部的PHP代码。

P.S. And yes, the calls that it is omit line numbers for are my own code, not internal PHP code.

推荐答案

<?
class BtTest
{
  public function getTheItem()
  {
    var_dump( debug_backtrace( false ) );
    $bt = debug_backtrace( false );
    return $bt[1];
  }

  public function __call( $methodName, $methodArgs )
  {
    return $this->getTheItem();
  }
}

$o = new BtTest();
$bti = $o->test();

assert( 'array_key_exists("function", $bti)' );
assert( 'array_key_exists("line", $bti)' );
assert( 'array_key_exists("file", $bti)' );

以上示例的执行生成以下输出:

The execution of above example generates following output:

array(3) {
  [0]=>
  array(6) {
    ["file"]=>
    string(53) "/somewhere/in/the/filesystem/tests/bt-test-so.php"
    ["line"]=>
    int(13)
    ["function"]=>
    string(10) "getTheItem"
    ["class"]=>
    string(6) "BtTest"
    ["type"]=>
    string(2) "->"
    ["args"]=>
    array(0) {
    }
  }
  [1]=>
  array(4) {
    ["function"]=>
    string(6) "__call"
    ["class"]=>
    string(6) "BtTest"
    ["type"]=>
    string(2) "->"
    ["args"]=>
    array(2) {
      [0]=>
      &string(4) "test"
      [1]=>
      &array(0) {
      }
    }
  }
  [2]=>
  array(6) {
    ["file"]=>
    string(53) "/somewhere/in/the/filesystem/tests/bt-test-so.php"
    ["line"]=>
    int(18)
    ["function"]=>
    string(4) "test"
    ["class"]=>
    string(6) "BtTest"
    ["type"]=>
    string(2) "->"
    ["args"]=>
    array(0) {
    }
  }
}
PHP Warning:  assert(): Assertion "array_key_exists("line", $bti)" failed in /somewhere/in/the/filesystem/tests/bt-test-so.php on line 21
PHP Warning:  assert(): Assertion "array_key_exists("file", $bti)" failed in /somewhere/in/the/filesystem/tests/bt-test-so.php on line 22

第一个追溯项索引0)间接地说(通过文件项), getTheItem 方法从 __调用方法调用。

The first backtrace item (index 0) says indirectly (through the line and file items) that the getTheItem method was called from the __call method.

第二个追溯项(索引1)表示 c code code code code code code $ c $ >文件项目)

The second backtrace item (index 1) says that the __call method was called from somewhere (missing line and file items).

第三个追溯项(索引2)表示测试方法从脚本的全局范围调用。

The third backtrace item (index 2) says that the test method was called from the global scope of the script.

方法调用 __调用的位置可能是在某些方法分辨率在php解释器代码的某个地方。有两种修复方法。第二个项目应该引用解释器的源代码文件和行,或者将第二个和第三个追溯项目合并为一个。我个人更喜欢第二个解决方案,因为解释器的内部对我来说不是有趣的(这是他们似乎在python的追溯中做的),但是我明白有时第一个解决方案提供了更明确的跟踪(特别是当它是一个回调,从内部调用)。

The place of the __call method call is probably in some method resolution code somewhere in the php interpreter code. There are two possibilities of fixing it. Either the second item should refer interpreter's source code file and line or the second and the third backtrace items should be merged into one. I personally would prefer the second solution as the interpreter's internals are not interesting for me (this is how they seem to do it in python's traceback), however I understand that sometimes the first solution provides more explicit trace (especially when it's a callback which is called from the internals).

所以,似乎开发人员负责(或至少维护) debug_backtrace 函数不会将其视为错误,也可能没有简单的方法来解决它。可以填写文件项目与某些占位符值(例如 < unknown-file> 0 甚至 nulls ),并在文档中强调它。除非有人成功说服他们这样做,否则你只需要处理代码中的特殊情况。

So or so, it seems that the developer(s) responsible for (or at least maintaining) the code of the debug_backtrace function doesn't perceive it as a bug or maybe has no easy way to fix it. It would be ok to fill the line and file items with some place holder values (e.g. <unknown-file> and 0 or even nulls) and emphasize it in the documentation. Unless someone will successfully convince them to do it, you just have to handle the special case in your code.

上面我写的只是为了分享我对这个功能的奇怪行为的理解。如果有人愿意为一个更好的世界而战,这里是一些相关错误报告的链接:

I wrote above just to share my understanding of the strange behaviour of the function. If someone has a willingness to fight for a slightly better world, here are links to some related bug reports:

  • #39070 debug_backtrace output when call_user_func or error handler involved
  • #24214 debug_backtrace() fails to report __FILE__, __LINE__
  • #24405 debug_backtrace - missing info
  • #38047 "file" and "line" sometimes not set in backtrace from inside error handler
  • #44428 "file" and "line" missing in debug_backtrace() output

最早的报告是从2003年开始,所以不要指望快速修复:)

The oldest report is from 2003, so you shouldn't count on a fast fix :)

这篇关于为什么debug_backtrace()有时不包括行号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆