tags:

views:

273

answers:

6

I know this is not exactly reflection, but kind of. I want to make a debug function that gets a variable and prints a var_dump and the variable name.

Of course, when the programmer writes a call to the function, they already know the variable's name, so they could write something like:

debug( $myvar, 'myvar' );

But I want it to be quick and easy to write, just the function name, the variable, and voilà !

debug( $myvar ); // quicker and easier :)
+4  A: 

Nope, not possible. Sorry.

chaos
:'( ...........
Petruza
Aww, don't be sad, little Lego person. Your two-argument `debug()` isn't that bad.
chaos
Thanks. I'm a Playmobil by the way.
Petruza
+1  A: 

No, the closer you will get is with get_defined_vars().

EDIT: I was wrong, after reading the user comments on get_defined_vars() it's possible with a little hack:

function ev($variable){
    foreach($GLOBALS as $key => $value){
        if($variable===$value){
            echo '<p>$'.$key.' - '.$value.'</p>';
        }
    }
}

$lol = 123;

ev($lol); // $lol - 123

Only works for unique variable contents though.

Alix Axel
Unique variable contents that are globals. Lovely false positive rate there.
chaos
Indeed, better than nothing though. =\
Alix Axel
Debatable.
chaos
Of course, but not in the mood for further debates. =P
Alix Axel
+2  A: 

i've had the same thought before, but if you really think about it, you'll see why this is impossible... presumably your debug function will defined like this: function debug($someVar) { } and there's no way for it to know the original variable was called $myvar.

The absolute best you could do would be to look at something like get_defined_vars() or $_GLOBALS (if it were a global for some reason) and loop through that to find something which matches the value of your variable. This is a very hacky and not very reliable method though. Your original method is the most efficient way.

nickf
It's not completely ridiculous to think that such a capability might be possible, but it would require `ReflectionParameter` having tracking for the source variable name *if* one exists (presumably it would be null when an rvalue is passed), and it doesn't.
chaos
Yes, I was like 99% sure it wasn't possible before making the question, but there's always someone on SO that knows more than you so...And thinking about how loosely typed and un-strict PHP is, this woudn't be such a crazy feature.Anyway, it's not _that_ useful, so me: stop dreaming.
Petruza
A: 

Not elegantly... BUT YOU COULD FAKE IT!

  • 1) Drink enough to convince yourself this is a good idea (it'll take a lot)
  • 2) Replace all your variables with variable variables:
$a = 10
//becomes
$a = '0a';
$$a = 10;
  • 3) Reference $$a in all your code.
  • 4) When you need to print the variable, print $a and strip out the leading 0.

Addendum: Only do this if you are

  • Never showing this code to anyone
  • Never need to change or maintain this code
  • Are crazy
  • Not doing this for a job
  • Look, just never do this, it is a joke
Tom Ritter
+1 for the comment, -2 for even considering it. there are naive people out there you know! ;-)
MiRAGe
A: 

You could use eval:

function debug($variablename)
{
  echo ($variablename . ":<br/>");
  eval("global $". $variablename . ";");
  eval("var_dump($" . $variablename . ");");
}

Usage: debug("myvar") not debug($myvar)

Waggers
??!?
chaos
not only does this not work (your function won't have the right scope), but there's a much better way to do the same thing with Variable Variables: `$$variablename`
nickf
I've changed the function to account for the loss of scope. I'm not to familiar with variable variables but will take your word for it!
Waggers
No need to eval, just use variable variables.
Alix Axel
yes, but this solution, as well as eyze's work only for globals. But thanks anyway!
Petruza
debug("a; mysql_query('DELETE FROM `users` WHERE 1');")
nickf
+1  A: 

Bit late to the game here, but Mach 13 has an interesting solution: How to get a variable name as a string in PHP

Rudu