debug_backtrace() 捕获函数参数的什么状态?
What state of function arguments does debug_backtrace() capture?
一些抽象代码:
function test($a = 5) {
debug_backtrace();
a = 10;
}
关于测试函数的参数,debug_trace 会告诉我们什么?
它会将 $a
捕获为 5 还是 10?
如果我们这样调用示例中的函数:
test(4);
它将捕获“4”。
如果我们这样称呼它:
test();
它实际上不会捕获任何关于参数的数据。我想如果参数没有在任何地方使用过,解析器就不会初始化参数。 (调用 debug_backtrace 不算。)
我做了更多的研究,如果通过引用传递参数,结果有点出乎意料(对我个人而言)......但我承认这很合乎逻辑。
如果我们使用下面的代码:
<?php
function test2(&$a) {
$a = 5;
test($a);
$a = 8;
}
function test(&$a) {
$a = 6;
print_r(debug_backtrace());
$a = 7;
}
$test = 1;
test2($test);
我们会得到这样的输出:
Array (
[0] => Array (
[file] => /var/www/localhost/htdocs/index.php
[line] => 4
[function] => test
[args] => Array ( [0] => 6 )
)
[1] => Array (
[file] => /var/www/localhost/htdocs/index.php
[line] => 13
[function] => test2
[args] => Array ( [0] => 6 )
)
)
所以 debug_backtrace() 总是打印引用传递的函数参数的当前状态(当实际调用 debug_backtrace() 时),无论它们在父函数调用中是否有另一个值。
调试时要小心! :)
一些抽象代码:
function test($a = 5) {
debug_backtrace();
a = 10;
}
关于测试函数的参数,debug_trace 会告诉我们什么?
它会将 $a
捕获为 5 还是 10?
如果我们这样调用示例中的函数:
test(4);
它将捕获“4”。
如果我们这样称呼它:
test();
它实际上不会捕获任何关于参数的数据。我想如果参数没有在任何地方使用过,解析器就不会初始化参数。 (调用 debug_backtrace 不算。)
我做了更多的研究,如果通过引用传递参数,结果有点出乎意料(对我个人而言)......但我承认这很合乎逻辑。
如果我们使用下面的代码:
<?php
function test2(&$a) {
$a = 5;
test($a);
$a = 8;
}
function test(&$a) {
$a = 6;
print_r(debug_backtrace());
$a = 7;
}
$test = 1;
test2($test);
我们会得到这样的输出:
Array (
[0] => Array (
[file] => /var/www/localhost/htdocs/index.php
[line] => 4
[function] => test
[args] => Array ( [0] => 6 )
)
[1] => Array (
[file] => /var/www/localhost/htdocs/index.php
[line] => 13
[function] => test2
[args] => Array ( [0] => 6 )
)
)
所以 debug_backtrace() 总是打印引用传递的函数参数的当前状态(当实际调用 debug_backtrace() 时),无论它们在父函数调用中是否有另一个值。
调试时要小心! :)