1

So, I need the actual variable name "varName" from global space. This function is actually a method inside a class.

Code is arbitrary for simplicity

$varName = 'what ever';

public function save($var)
{
    $i[varName goes here] = $var;
}

I don't know if this is even possible, but I think maybe with a callback?

RoboTamer
  • 3,474
  • 2
  • 39
  • 43

4 Answers4

1

If it's a global variable, you might be able to find out the original name with:

$varname = array_search($var, $GLOBALS);

But that's not overly reliable; a best guess. If two global variables would contain the same value, you would just receive the name of either of them.

mario
  • 144,265
  • 20
  • 237
  • 291
  • This solution is the only one that comes to my mind. There is still some solution to solve the issue with multiple results, I believe - you can perform what I have described in my answer. Although I do not know, if it will work... – Tadeck May 31 '11 at 00:21
  • 1
    @Tadeck: If you want to find multiple matching keys, that's likewise simple with `array_intersect($GLOBALS, array($var))`. – mario May 31 '11 at 00:25
  • @TaMeR incorrect - just consider situation when two variables have value equal to `'what ever'` – Tadeck May 31 '11 at 00:25
1

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:

  1. Pass variable to the method with reference.
  2. 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.
  3. Save the initial value of the variable and save the list of positions at which you have found matching elements.
  4. 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.
  5. 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)

Tadeck
  • 132,510
  • 28
  • 152
  • 198
0

In C, you would use a macro. I don't think there's an equivalent in PHP.

You could pass the name of the variable to the function as a string, then in the function get the variable's value from GLOBALS or eval it.

Paul Harrison
  • 455
  • 3
  • 6
0

Thanks for the solution! Here is my test:

<?php
$varName = 'what ever';

function save($var)
{
    $i[array_search($var, $GLOBALS)] = $var;
    print_r($i);
}
save($varName);
?>

prints:

Array
(
    [varName] => what ever
)
RoboTamer
  • 3,474
  • 2
  • 39
  • 43