Trick question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Trick question

Post 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? ;)
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post 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;
...
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

It's interesting how often a subject has been postulated before: viewtopic.php?t=51311&highlight=debugbacktrace+file
Post Reply