在每个 PHP 回溯级别获取范围内的变量? [英] Get variables in scope at each PHP backtrace level?
问题描述
有没有办法在回溯中查看每个堆栈帧中设置的变量?我可以非常接近 debug_backtrace(true)
的组合来获取对象,在每个对象上使用 get_object_vars
来获取 $this vars,args
> 在每个回溯帧中键入键,并使用 get_defined_vars
获取全局变量,但是在函数中设置的任何临时变量我都找不到检索方法.
Is there a way to view the variables set in each stack frame in a backtrace? I can come pretty close with a combination of debug_backtrace(true)
to get the objects, get_object_vars
on each object to get $this vars, the args
key in each backtrace frame, and get_defined_vars
to get globals, but any temporary variables set within a function I can't find a way to retrieve.
这是一个示例情况:
function method1($foo) {
$temp = method2($foo + 1);
foreach ($temp as $t) {
method2($t);
}
}
function method2($bar) {
$temp2 = $bar->value + $_GET['val'];
debug();
}
function debug() {
// to be created
$global_scope = get_defined_vars();
$bt = debug_backtrace(true);
}
我可以通过回溯中的args
键获得$foo
和$bar
,对象变量$bar
到 get_object_vars
,全局变量到 get_defined_vars
.我想要获得 $temp2
和 $temp
的值.
I can get $foo
and $bar
via the args
key in the backtrace, the object variables of $bar
through get_object_vars
, and the globals through get_defined_vars
. I want to get the value of $temp2
and $temp
as well.
推荐答案
在(本地)服务器上安装并启用 XDebug.然后使用 xdebug_get_declared_vars()
.确保在 xdebug .ini
文件中将 xdebug.collect_vars
设置为 On.
Install and Enable XDebug on your (local) server. Then use xdebug_get_declared_vars()
. Make sure that you set xdebug.collect_vars
to On in your xdebug .ini
file.
<?php
class strings {
static function fix_strings($a, $b) {
foreach ($b as $item) {
}
var_dump(xdebug_get_declared_vars());
}
}
strings::fix_strings(array(1,2,3), array(4,5,6));
?>
返回:
array
0 => string 'a' (length=1)
1 => string 'b' (length=1)
2 => string 'item' (length=4)
来自 xdebug.org
请注意,该函数仅返回调用函数 xdebug_get_declared_vars()
的范围内的变量.
Note, that the function only returns variables in the scope where the function xdebug_get_declared_vars()
is called in.
这篇关于在每个 PHP 回溯级别获取范围内的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!