2

在处理我的一个项目时,我在代码文件的开头PHP得到了一个函数,即.debug_backtrace()<?php debug_backtrace() || die ("Direct access not permitted"); ?>

在研究它时,我得到了一些解释,它的工作原理是:

在 Drupal 网站中调试 PHP 问题可以是快速简单的,也可以是严重的问题。PHP 包含一个名为 debug_backtrace 的调试函数,它将打印出导致调用 backtrace 函数的位置的代码链。

当我使用var_dump()withdebug_backtrace()我得到以下结果:

array(2) {
  [0]=>
  array(3) {
    ["file"]=>
    string(61) "C:\xampp\htdocs\folder_name\templates\default\models\home.php"
    ["line"]=>
    int(30)
    ["function"]=>
    string(7) "include"
  }
  [1]=>
  array(4) {
    ["file"]=>
    string(37) "C:\xampp\htdocs\folder_name\index.php"
    ["line"]=>
    int(146)
    ["args"]=>
    array(1) {
      [0]=>
      string(61) "C:\xampp\htdocs\folder_name\templates\default\models\home.php"
    }
    ["function"]=>
    string(7) "include"
  }
}

我不知道什么debug_backtrace()功能到底是有效的。

请任何人解释是受欢迎的。提前致谢。

学习链接:

来自 PHP 中注册的关闭函数的 debug_backtrace()

使用 debug_backtrace 调试 PHP 代码

将 debug_backtrace() 分配给 PHP 中的变量

打印 PHP 调用堆栈

如何将 PHP 回溯保存到错误日志?

4

2 回答 2

6

举一个基本的例子...

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

echo "Start...";
print_r(debug_backtrace());

function t1 ()  {
    echo "Start t1...";
    print_r(debug_backtrace());

}

function t2()   {
    echo "Start t2...";
    print_r(debug_backtrace());
    t1();
}

echo "before calls...";
print_r(debug_backtrace());
t1();
t2();

会输出...

Start...Array
(
)
before calls...Array
(
)
Start t1...Array
(
    [0] => Array
        (
            [file] => /home/nigel/workspace/PHPTest/TestSource/part3.php
            [line] => 22
            [function] => t1
            [args] => Array
                (
                )

        )

)
Start t2...Array
(
    [0] => Array
        (
            [file] => /home/nigel/workspace/PHPTest/TestSource/part3.php
            [line] => 23
            [function] => t2
            [args] => Array
                (
                )

        )

)
Start t1...Array
(
    [0] => Array
        (
            [file] => /home/nigel/workspace/PHPTest/TestSource/part3.php
            [line] => 17
            [function] => t1
            [args] => Array
                (
                )

        )

    [1] => Array
        (
            [file] => /home/nigel/workspace/PHPTest/TestSource/part3.php
            [line] => 23
            [function] => t2
            [args] => Array
                (
                )

        )

)

我希望这表明该debug_backtrace函数所做的只是返回到目前为止所调用的内容。所以当你第一次调用时t1,它只是调用的堆栈跟踪t1。开头也是一样t2,但是在t2调用时t1,跟踪会列出对t2and的调用t1

但也如您所见,debug_backtraceStart..没有显示任何内容,因为没有导致打印此跟踪的代码级别。

在您的情况下,它调用debug_backtrace并使用or die()'如果不debug_backtrace返回任何内容,则die()'

于 2017-08-23T08:18:05.713 回答
0

我认为 debug_backtrace() 最有用的方法是跟踪哪个文件是 init 启动核心,例如

a.php
include 'b.php';

b.php
function test(){
     echo('whatever');
}
print_r(debug_backtrace())

php a.php


whateverArray
(
    [0] => Array
        (
            [file] => /opt/php/b.php
            [line] => 7
            [function] => test
            [args] => Array
                (
                )

        )

    [1] => Array
        (
            [file] => /opt/php/a.php
            [line] => 3
            [args] => Array
                (
                    [0] => /opt/php/b.php
                )

            [function] => include
        )

)

所以,你可以从

$backtrace = debug_backtrace()
echo $backtrace[count($backtrace) - 1]['file'];

获取启动调用的文件

于 2021-07-26T16:55:38.997 回答