2

Not sure how to ask this without rambling about preprocessors in C, but I will try:

Is there a way in PHP, to use the name of a variable as a string or value.

Such that instead of calling function like this,

$somevar = 'whatever';

fun('somevar', $somevar);

you can call it like this:

fun(<magic:$somevar>, $somevar);

or even, dare I say it, like this:

fun($somevar);

and the fun() would itself deduce both the value ('whatever'), and the name ('somevar') of the variable. (In C, it could be done with a preprocessor #define.)

"magic" above is of course a place holder for the magic syntax in PHP I am looking for which will help me do this. I guess it could be made somehow with eval()? But people keep telling me not to use eval.

Someone hinted that introspection might somehow do the trick. Any leads there would be welcome.

8
  • I got some hints about ReflectionParameters by passers-by, but you deleted your comments. Commented Sep 6, 2012 at 7:24
  • What are you trying to accomplish? Commented Sep 6, 2012 at 7:28
  • @Amigable are you looking for something like returning the variable name as a value.? Commented Sep 6, 2012 at 7:29
  • @sberry, more succinct code, I have lots of variable names which are always the same as that string. Stupid to type the same thing twice. Commented Sep 6, 2012 at 7:29
  • 1
    @goldenparrot, from the description, technically yes, but actually not quite, what happens if I mistype, fun(ltrim('$somevarr', '$'), $somevar); is that the PHP gives no help at all. If the first argument was just a variable, the parser could warn that $somevarr was undefined. Commented Sep 6, 2012 at 7:58

3 Answers 3

2

Try this

function var_name(&$var, $scope=0)
{
    $old = $var;
    if (($key = array_search($var = 'unique'.rand().'value', !$scope ? $GLOBALS : $scope)) && $var = $old) return $key;  
}
Sign up to request clarification or add additional context in comments.

13 Comments

This isn't quite what he is looking for
@Mythril i think he wants the variable name to be as a string. This function returns that. or can u please explain where I'm wrong so that i correct it.
Run this in the same scope as your function $x = 5; $y = 5; echo var_name($x), "\n"; echo var_name($y), "\n";
also, this would utterly fail when trying to use it on a local inside of a function
If nothing better performance wise shows up, I will eventually accept this answer, because your function works. But I won't use it, it's too weird. :-)
|
2

http://php.net/manual/en/language.variables.variable.php

The probably explain it better than I can, I think this is what you're looking for?

Alright, before you call the function do the following:

$var= "variableName";
$$var = "variableValue";
fun($var);

Now within the function definition for fun:

function fun($parameter)
{
    global $$parameter;
    echo $parameter; //echos the variableName
    echo $$parameter; //echos the variableValue
}

7 Comments

Either, no it's NOT what I am looking for, OR I don't understand what that page says.
@RedHydra: I would have given the same answer
@Double so you would also have pointed to a page and not actually write an answer. ;-)
@AmigableClarkKant I've updated my answer, I'm pretty sure this will work for you, let me know if it doesn't.
How will that help me write: $clevername = 'something'; fun($clevername); ....... and the fun() knowing both 'clevername' and 'something'
|
1

No. PHP's scoping rules prevent you from doing that. The only way would be through a pre processor (as you are aware) or by introspecting the code, which would be kind of the same thing. PHP has a built-in tokenizer, that you can use to parse a file with. You can use debug_backtrace to find out which files to parse through. It's going to be a really messy and inefficient thing to do though, so you would probably be better off rethinking what you're doing.

4 Comments

Elan - "I don't wanna sell you death sticks." troelskn - "You want to go home and rethink your life." Amigable - "I wanna go home and rethink my life."
;) - Not knowing your use case, I may be off, but it sounds like you might want to use an array for this, somehow?
I think I will just use fun('x', $x); at least it's very readable and straightforward and easy to spot errors visually.
If it works, then I concur. Verbose is usually better than clever.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.