Read about $GLOBALS
variable in the documentation.
This is probably what you need. Depending on the way you determine which variable you need, you can for example user array_search()
to find the proper name based on the value.
Caution: $GLOBALS
is about global scope's variables.
EDIT:
But this would be still a guess. You may try the following method to determine the name of the passed variable with ~100% certainty:
- Pass variable to the method with reference.
- Use
array_search()
for finding the name of the variable. If only one key matches it, you have your name. If not, go to the next step.
- Save the initial value of the variable and save the list of positions at which you have found matching elements.
- Change the variable's value into new one. Perform another search based on new value and get positions that are also in the list of positions from point no. 3.
- At this point you have probably found the name of the variable you are looking for.
But...
Is it really needed? I suggest that you should look for simpler solution, some better encapsulation of your code.
Ps. array_search()
actually returns no more than one key (see documentation). You should know that and make searching for multiple results a little more sophisticated to not skip the correct one if more than one variable matches your search criteria. (EDIT2: As mario suggested, array_intersect($GLOBALS, array($var))
will suffice)