In PHP, how can I get the variable name that passe

2019-08-08 17:35发布

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

If I can get "$this->_classResources" then I don't need the second params.

2条回答
ゆ 、 Hurt°
2楼-- · 2019-08-08 17:57

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.

查看更多
乱世女痞
3楼-- · 2019-08-08 18:01
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.

查看更多
登录 后发表回答