【问题标题】:What the debug_backtrace() exactly works in php?debug_backtrace() 在 php 中到底有什么作用?
【发布时间】:2018-01-31 14:41:34
【问题描述】:

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

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

在 Drupal 网站中调试 PHP 问题可以是快速的任何事情 并且容易出现严重问题。 PHP 包含调试功能 调用 debug_backtrace ,它将打印出代码链 直到调用回溯函数的地方。

当我将var_dump()debug_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() 函数的确切作用。

欢迎任何人解释。提前致谢。

学习链接:

debug_backtrace() from registered shutdown function in PHP

Debugging PHP Code with debug_backtrace

Assign debug_backtrace() To a variable in PHP

Print PHP Call Stack

How can I save a PHP backtrace to the error log?

【问题讨论】:

  • 您的意思是在这种情况下或一般情况下它在做什么?在这种情况下,如果脚本没有作为应用程序的一部分调用,则没有应用程序回溯,因此它会阻止您仅运行该特定脚本。
  • 正如你所说,在这种情况下它会阻止运行脚本,这意味着它作为会话工作?我对这个函数完全陌生:debug_backtrace(),请你解释一下它的一般工作原理以及它在我的情况下是如何工作的。

标签: php debugging error-handling core debug-backtrace


【解决方案1】:

举一个基本的例子......

<?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 时,跟踪会列出对t2t1 的调用。

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

在您的情况下,它调用debug_backtrace 并使用or die() 说'如果debug_backtrace 什么都不返回,那么die()'

【讨论】:

【解决方案2】:

我认为 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-10-30
    • 2017-07-26
    • 2011-04-18
    • 2022-11-29
    • 1970-01-01
    • 2019-11-03
    • 2010-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多