function to figure out name of passed variable?

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
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

function to figure out name of passed variable?

Post by Skara »

I was wondering if there were a way to do something like this:

Code: Select all

function debug($var) {
  print([name of variable] . ' = ' . $var);
}
$example = 'test';
debug($example);
//output:
//$example = test
Basically, I want to know the name of the variable passed to debug(). Is that possible?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

not really possible, in the generally feasible and usable sense.
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

Alright, thanks.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

but in infeasible and unusable sense it would look like:

Code: Select all

function debug($var) {
    $trace = debug_backtrace();
    $file = file($trace[0]['file']);
    $line = $file[$trace[0]['line']-1];
    preg_match('/debug\s*\(([^)]+)\)/i', $line, $matches);
    echo $matches[1] . ' = ' . var_export($var, true) . "\n";
}
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Thats funny.

Edit: I mean the infeasible and unusable vs generally feasible and usable comments
Last edited by Benjamin on Sat Jul 08, 2006 1:47 pm, edited 1 time in total.
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

actually thanks for that function. im sure ill find a use for it.
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

hm. Well, thanks but no thanks. Much more than I want. :P
Interesting, though.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Weirdan wrote:but in infeasible and unusable sense it would look like:

Code: Select all

function debug($var) {
    $trace = debug_backtrace();
    $file = file($trace[0]['file']);
    $line = $file[$trace[0]['line']-1];
    preg_match('/debug\s*\(([^)]+)\)/i', $line, $matches);
    echo $matches[1] . ' = ' . var_export($var, true) . "\n";
}
Whoa!

*coughs*

That is very impressive.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

That is very impressive.
Not really. It does very naive function call parsing, without support for complex expressions with parens within. It will break as soon as you call it with something like this:

Code: Select all

debug("some)");
// would echo:
// "some = 'some)';
debug(debug_backtrace());
// would echo:
// debug_backtrace( = array(....)
Moreover, for large files it's very memory intensive (because file() is used).

It was intended to be joke and not suitable to be used even in development environment (not that you want to use functions like debug() on a production server :) )
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

it was intended to be joke and not suitable to be used even in development environment (not that you want to use functions like debug() on a production server Smile )
Oh absolutely. What is impressive is that you knew about it and how to use it because its pretty useless. I think the guys who make php just like to have a laugh every now and then.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

What is impressive is that you knew about it and how to use it because its pretty useless
Err... what is useless? knew about what?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Err... what is useless? knew about what?
function debug($var)
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

i would have to disagree on the whole useless thing, if its included in the base php library, i guarentee that it has a use, it may be a very minute use but a use none the less. there is some point in ur life where u will be saved by a "useless" function like debug_backtrace()
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

The example I used above is somewhat useless. The real thing is a bit more complicated. In any case, I just wanted to use it as a timesaver. *shrugs*
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

ole wrote: function debug($var)
I didn't know about it, I wrote it :) And yes, it's useless and has some flaws as I pointed out.
Post Reply