lovespring
lovespring

Reputation: 19589

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

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

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

Upvotes: 1

Views: 582

Answers (2)

Frederik.L
Frederik.L

Reputation: 5620

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.

Upvotes: 0

Andy Ray
Andy Ray

Reputation: 32076

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.

Upvotes: 3

Related Questions