我正在编写一种方法来生成用于在调试模式下记录函数和参数的详细数据。我之前在多个地方复制了相同的代码(不好),但它工作得很好(好):
function validate_date($date)
{
if ($condition)
{
$php_function = __FUNCTION__;
$php_function_args = implode(', ',get_func_argNames($php_function));
foreach (get_func_argNames($php_function) as $arg)
{
$txt.= "$arg: ${$arg}<br>";
}
}
}
所以我写了一个新方法来使它更容易维护:
$_debug_txt = return_debug_header(__FUNCTION__);
function return_debug_header($php_function)
{
// returns debug string to debug handler
$arr_args = get_func_argNames($php_function);
$php_function_args = implode(', ',$arr_args);
if (is_array($arr_args)) {
foreach ($arr_args as $arg)
{
// $arg shows the right variable NAME, but ${$arg} is null.
$txt.= "$arg: ${$arg}<br>";
}
} else {
$txt = 'No arguments passed.';
}
可以这样使用
function validate_date($date)
{
if ($condition)
{
// generate debug header only if debug is true.
$_debug_txt = return_debug_header(__FUNCTION__);
// do something with txt...
}
}
问题是变量变量似乎不适用于从get_func_argNames
. 变量名称存在(我可以将它们打印到屏幕上),但相应的值显示为空白。
PHP 警告变量变量不能与 superglobals 一起使用,但是不清楚从 get_func_argNames 返回的数据是否被认为是“超全局”。
有没有人看到任何其他可能导致变量变量在这个函数中不起作用的东西?