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

2019-08-08 17:45发布

问题:

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

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

回答1:

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.



回答2:

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.