Press "Enter" to skip to content

How to Get the Name of a Variable

For example, suppose I provide a query service where users can submit a person's name and age as query conditions.
Say I want to query a person named "laruence" who is 27 years old. I think the query token defining this person could be written as:

   laruence=27

Unfortunately, when such a token is submitted to the server's handler script as a query string, you'll find that, well... I don't know what the user's name is...
Alright, so you have to write it like this instead:

username=laruence&age=27

So, can we obtain a variable's name?
First, let's analyze the feasibility.
We know that in C, all symbols are "replaced" by the compiler.
In PHP, all variables are stored in a HashTable structure called the "symbol table". During parsing and execution, the "symbol" information is still retained, so it is definitely possible to obtain it.
In PHP, a symbol's scope is associated with the active symbol table. At any given time, there is only one active symbol table.
So how do we understand the active symbol table versus the symbol table?
For PHP, the currently active symbol table is stored in the global variable EG(active_symbol_table). At the same time, there is also a global symbol table stored in EG(symbol_table). Before entering the execution body of a function call, a new active_symbol_table is generated, and a call-stack-style symbol table stack EG(symtable_cache) is maintained, so that when exiting the function call, the previous active symbol table (scope) can be restored.
Meanwhile, in PHP, scope inheritance cannot be implemented — that is, you cannot directly access symbols from the outer scope (you need to add a global declaration). And if you do add the global declaration, a copy is also generated in the current active scope. In other words, there is no symbol visible in the current scope that is stored in the global symbol table.
From the analysis above, we only need to look in the current active symbol table to find the name of the variable we want.
Of course, that's not enough. How do we get the current symbol table within a PHP script?

  get_defined_vars
 

However, there's a caveat: get_defined_vars returns the variable names defined in the current active symbol table. That means if you need to wrap a function like:

  get_variable_name($var)

and try to use get_defined_vars within that function to get the symbol table at the moment get_variable_name is called, it won't work.
So our function for getting a variable's name should look like this:

  get_variable_name($var, $scope)

Now that we have the current active symbol table, how do we get the variable's name?
Obviously, we need to query this table based on the variable's value, finding the variable whose value equals the target value. But doing so raises another problem: multiple variables might have equal values.
So we need to give this variable a unique value. And to make this variable work, the form of our target function has to change again:

  get_variable_name(&$var, $scope)

Next, let's flesh out the function body:

function get_variable_name(&$var, $scope = NULL) {
       if (NULL == $scope) {
          $scope = $GLOBALS;
       }
       $tmp  = $var;
       $var   = "tmp_exists_" . mt_rand();
       $name = array_search($var, $scope, TRUE);
       $var   = $tmp;
       return $name;
}

Another issue is that if multiple variables are references to each other, this function only returns the name of the first-defined variable...
Also, you can refer to: http://php.net/manual/en/language.variables.php
For more on scope, you can also see my earlier article: Understanding PHP Internals: Variable Scope (Scope in PHP)

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.