PHP 中的 debug_backtrace() 函数
phpprogrammingserver side programming更新于 2025/11/20 16:32:00
debug_backtrace() 函数生成回溯。它返回一个关联数组。可能返回的元素如下 −
| 名称 | 类型 | 描述 |
|---|---|---|
| function | string | 当前函数名称。 |
| line | integer | 当前行号。 |
| file | string | 当前文件名。 |
| class | string | 当前类名称。 |
| object | string | 当前对象。 |
| type | string | 当前调用类型。如果是方法调用,则返回"->"。如果是静态方法调用,则返回"::"。如果是函数调用,则不返回任何内容。 |
| arg | 数组 | 如果在函数内部,则列出函数参数。如果在包含文件中,则列出包含的文件名。 |
语法
debug_backtrace(options, limit)
参数
选项 −以下给出的选项的位掩码 −
- DEBUG_BACKTRACE_PROVIDE_OBJECT:是否填充"object"索引
- DEBUG_BACKTRACE_IGNORE_ARGS:是否省略"args"索引和所有函数/方法参数以节省内存。
limit − 限制打印的堆栈帧数量
返回
debug_backtrace() 函数返回一个关联数组。可能返回的元素如上所述。
示例
下面是一个例子 −
<?php
function display($str) {
echo "Hi: $str";
var_dump(debug_backtrace());
}
display('hello');
?>
输出
以下是输出 −
Hi: helloarray(1) {
[0]=>
array(4) {
["file"]=> string(36) "/var/www/tutorialspoint/php/test.php"
["line"]=> int(8)
["function"]=> string(8) "printStr"
["args"]=> array(1) {
[0]=>
&string(6) "hello"
}
}
}

