假设我有这样的PHP代码:
$FooBar = "a string";
然后我需要一个这样的函数:
print_var_name($FooBar);
打印:
FooBar
有什么想法如何实现这一点?这在PHP中可能吗?
假设我有这样的PHP代码:
$FooBar = "a string";
然后我需要一个这样的函数:
print_var_name($FooBar);
打印:
FooBar
有什么想法如何实现这一点?这在PHP中可能吗?
当前回答
许多回复质疑这样做的用处。然而,获取变量的引用是非常有用的。特别是在有对象和$this的情况下。我的解决方案适用于对象,以及属性定义的对象:
function getReference(&$var)
{
if(is_object($var))
$var->___uniqid = uniqid();
else
$var = serialize($var);
$name = getReference_traverse($var,$GLOBALS);
if(is_object($var))
unset($var->___uniqid);
else
$var = unserialize($var);
return "\${$name}";
}
function getReference_traverse(&$var,$arr)
{
if($name = array_search($var,$arr,true))
return "{$name}";
foreach($arr as $key=>$value)
if(is_object($value))
if($name = getReference_traverse($var,get_object_vars($value)))
return "{$key}->{$name}";
}
上面的例子:
class A
{
public function whatIs()
{
echo getReference($this);
}
}
$B = 12;
$C = 12;
$D = new A;
echo getReference($B)."<br/>"; //$B
echo getReference($C)."<br/>"; //$C
$D->whatIs(); //$D
其他回答
似乎没有人提到这是a)困难和b)不明智的根本原因:
A "variable" is just a symbol pointing at something else. In PHP, it internally points to something called a "zval", which can actually be used for multiple variables simultaneously, either because they have the same value (PHP implements something called "copy-on-write" so that $foo = $bar doesn't need to allocate extra memory straight away) or because they have been assigned (or passed to a function) by reference (e.g. $foo =& $bar). So a zval has no name. When you pass a parameter to a function you are creating a new variable (even if it's a reference). You could pass something anonymous, like "hello", but once inside your function, it's whatever variable you name it as. This is fairly fundamental to code separation: if a function relied on what a variable used to be called, it would be more like a goto than a properly separate function. Global variables are generally considered a bad idea. A lot of the examples here assume that the variable you want to "reflect" can be found in $GLOBALS, but this will only be true if you've structured your code badly and variables aren't scoped to some function or object. Variable names are there to help programmers read their code. Renaming variables to better suit their purpose is a very common refactoring practice, and the whole point is that it doesn't make any difference.
现在,我理解这种调试的愿望(尽管一些建议的用法远远超出了这一点),但作为一种通用的解决方案,它实际上并没有你想象的那么有用:如果你的调试函数说你的变量是“$file”,那仍然可能是你代码中数十个“$file”变量中的任何一个,或者一个你称为“$filename”的变量,但传递给一个参数为“$file”的函数。
更有用的信息是在代码中调用调试函数的位置。因为你可以在你的编辑器中快速找到它,你可以看到你为自己输出的变量,甚至可以一次性将整个表达式传递给它(例如debug('$foo + $bar = ')。($foo + $bar))。
为此,你可以在调试函数的顶部使用这段代码:
$backtrace = debug_backtrace();
echo '# Debug function called from ' . $backtrace[0]['file'] . ' at line ' . $backtrace[0]['line'];
我有这个:
debug_echo(array('$query'=>$query, '$nrUsers'=>$nrUsers, '$hdr'=>$hdr));
我更喜欢这样:
debug_echo($query, $nrUsers, $hdr);
现有函数显示一个带有红色轮廓的黄色框,并按名称和值显示每个变量。数组解决方案是可行的,但在需要时输入有点复杂。
这就是我的用例,是的,它确实与调试有关。我同意那些质疑其其他用途的人。
这正是你想要的-这是一个随时可以使用的“复制并导入”函数,它会回显给定变量的名称:
function print_var_name(){
// read backtrace
$bt = debug_backtrace();
// read file
$file = file($bt[0]['file']);
// select exact print_var_name($varname) line
$src = $file[$bt[0]['line']-1];
// search pattern
$pat = '#(.*)'.__FUNCTION__.' *?\( *?(.*) *?\)(.*)#i';
// extract $varname from match no 2
$var = preg_replace($pat, '$2', $src);
// print to browser
echo '<pre>' . trim($var) . ' = ' . print_r(current(func_get_args()), true) . '</pre>';
}
用法:print_var_name ($ FooBar)
打印:FooBar
提示 现在你可以重命名函数,它仍然可以工作,也可以在一行中多次使用该函数!感谢@Cliffordlife 我添加了一个更好的输出!多亏了@Blue-Water
您可以使用get_defined_vars()来查找与您试图查找的变量名称具有相同值的变量的名称。显然这并不总是有效,因为不同的变量通常有相同的值,但这是我能想到的唯一方法。
编辑:get_defined_vars()似乎没有正确工作,它返回'var',因为$var在函数本身中使用。$GLOBALS似乎可以,所以我把它改成了这个。
function print_var_name($var) {
foreach($GLOBALS as $var_name => $value) {
if ($value === $var) {
return $var_name;
}
}
return false;
}
编辑:需要明确的是,在PHP中没有好的方法来做到这一点,这可能是因为您不应该这样做。也许有更好的方法来做你想做的事情。
我也想不出有效的方法但我想到了这个。对于下面的有限用途,它是有效的。
耸耸肩
<?php
function varName( $v ) {
$trace = debug_backtrace();
$vLine = file( __FILE__ );
$fLine = $vLine[ $trace[0]['line'] - 1 ];
preg_match( "#\\$(\w+)#", $fLine, $match );
print_r( $match );
}
$foo = "knight";
$bar = array( 1, 2, 3 );
$baz = 12345;
varName( $foo );
varName( $bar );
varName( $baz );
?>
// Returns
Array
(
[0] => $foo
[1] => foo
)
Array
(
[0] => $bar
[1] => bar
)
Array
(
[0] => $baz
[1] => baz
)
它基于调用函数的行来工作,在那里它可以找到传入的参数。我认为它可以扩展到多个参数,但是,就像其他人说的,如果你能更好地解释情况,另一个解决方案可能会更好。