0

这有点难以解释,但代码可能更清晰:

// class.php
class Foo
{
    public function bar ()
    {

    }
}


// test.php
$foo = new Foo;
$foo->bar(); // e.g., for some reason this returns an error hence error handler will be triggered

这是一个简化的示例,但 test.php 的嵌套可能更深。我的自定义错误处理程序如何告诉我错误发生在 test.php 第 2 行?

我目前正在使用,但 test.php 的数组索引会根据对象的debug_backtrace()深度或数量而有所不同require()

不管函数调用的嵌套有多深,有没有办法查明这一点?

4

1 回答 1

1

您可以打印一个debug_backtrace()将生成所有调用者的完整数组,包括文件和行号。

例子

<?php

    class Test {

        public function debug() {
            print_r(debug_backtrace());
        }
    }

    function print_debug() {
        $test = new Test();
        $test->debug();
    }

    header("Content-type: text/plain");
    print_debug();

结果

Array
(
    [0] => Array
        (
            [file] => D:\Websites\htdocs\tests\index.php
            [line] => 11
            [function] => debug
            [class] => Test
            [object] => Test Object
                (
                )

            [type] => ->
            [args] => Array
                (
                )

        )

    [1] => Array
        (
            [file] => D:\Websites\htdocs\tests\index.php
            [line] => 14
            [function] => print_debug
            [args] => Array
                (
                )

        )

)

你也可以尝试抛出一个异常,让它冒泡,它会杀死你的脚本,显示一个完整的回溯。看看这是否适合你。

于 2012-07-25T19:06:53.720 回答