简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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