Page 1 of 1

Trick question

Posted: Thu Nov 09, 2006 1:01 am
by alex.barylski
Edit: I've looked at debug_backtrace and it might work, but WOW would that be a hack :P

Edit2: Is it possible given the literal name of a variable '$obj' that I could use the literal name to retreive the content of the variable itself - despite this not making sense technically...maybe PHP has a weapon up it's sleeve?

------------------------------------------EOE----------------------------------------

I have a variable which I pass to a function...

Inside the function is there any way you can think of that would allow me to get the name of the original variable - as awkward or as hackaish as this sounds??? :P

Other than passing the name directly? ;)

Posted: Thu Nov 09, 2006 6:27 am
by Mordred
Huh, why would you want to do that? Sure there's no other way?

Anyway you can try something like this:
(i.e. Yes - you pass the name directly)

Code: Select all

function Increase($varname) {
   ++$GLOBALS[$varname]; //global
   ++$this->$varname; //member
 //or, for more complex stuff (i.e. not a one-liner) use aliases:
  $pVar =& $GLOBALS[$varname]; //p = pointer, sorry, I come from a C++ neighbourhood 
  ++$pVar;
  --$pVar;
  $pVar *= 2;
...
}

Posted: Thu Nov 09, 2006 6:59 am
by feyd
It's interesting how often a subject has been postulated before: viewtopic.php?t=51311&highlight=debugbacktrace+file