Page 1 of 1

function to figure out name of passed variable?

Posted: Sat Jul 08, 2006 12:23 pm
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?

Posted: Sat Jul 08, 2006 12:33 pm
by feyd
not really possible, in the generally feasible and usable sense.

Posted: Sat Jul 08, 2006 12:39 pm
by Skara
Alright, thanks.

Posted: Sat Jul 08, 2006 1:09 pm
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";
}

Posted: Sat Jul 08, 2006 1:12 pm
by Benjamin
Thats funny.

Edit: I mean the infeasible and unusable vs generally feasible and usable comments

Posted: Sat Jul 08, 2006 1:29 pm
by dull1554
actually thanks for that function. im sure ill find a use for it.

Posted: Sat Jul 08, 2006 1:52 pm
by Skara
hm. Well, thanks but no thanks. Much more than I want. :P
Interesting, though.

Posted: Sat Jul 08, 2006 3:37 pm
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.

Posted: Sat Jul 08, 2006 5:55 pm
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 :) )

Posted: Sat Jul 08, 2006 6:05 pm
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.

Posted: Sat Jul 08, 2006 6:14 pm
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?

Posted: Sat Jul 08, 2006 6:34 pm
by Ollie Saunders
Err... what is useless? knew about what?
function debug($var)

Posted: Sat Jul 08, 2006 6:36 pm
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()

Posted: Sat Jul 08, 2006 6:49 pm
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*

Posted: Sat Jul 08, 2006 7:31 pm
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.