简体   繁体   English

在PHP中,如何获取函数调用中传入的变量名?

[英]In PHP, how can I get the variable name that passed in in a function call?

trace($this->_classResources,'$this->_classResources');

如果我可以获得“ $ this-> _ classResources”,则不需要第二个参数。

You can't, and shouldn't be able to. 你不能,也不应该。 There is probably a different way you could structure your code to get around this problem. 您可以通过不同的方式构建代码以解决此问题。

function print_var_name($var) {
    foreach($GLOBALS as $var_name => $value) {
        if ($value === $var) {
            return $var_name;
        }
    }
    return false;
}

This function will not directly get the variable name but it will scan for any variable name that contains the same value as the one you specify. 此函数不会直接获取变量名称,但会扫描包含与指定值相同的任何变量名称。 If you can ensure that values passed in your function are different from all other values, it should do the job. 如果您可以确保函数中传递的值与所有其他值不同,那么它应该完成这项工作。 The crappy method that could work is to set a prefix for the values that will be passed in this function. 可行的糟糕方法是为将在此函数中传递的值设置前缀。 Once in the function, you just skip the prefix part with substr. 进入函数后,您只需使用substr跳过前缀部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM