在处理我的一个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()
函数到底能起什么作用。
欢迎任何人解释。提前谢谢。
所研究的链接:
backtrace() from registered shutdown function in PHP
发布于 2017-08-23 08:18:05
举个基本的例子..。
<?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
时,跟踪会列出对t2
和t1
的调用。
但是,正如您所看到的,来自debug_backtrace
的Start..
没有显示任何东西,因为没有任何级别的代码会导致这个跟踪被打印出来。
在您的例子中,它调用debug_backtrace
,并使用or die()
来表示‘如果debug_backtrace
不返回任何东西,那么die()
’
发布于 2021-07-26 16:55:38
我认为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'];
获取启动呼叫的文件
https://stackoverflow.com/questions/45832409
复制相似问题