tags:

views:

88

answers:

4

I'm wondering if there's any way to do the following:

function whatever($parameter)
{  
    echo funky_reflection_function();
}

whatever(5 == 7); // outputs "5 == 5" (not true or 1)

I'm not optimistic, but anyone know if theres any crazy hack I can do to do this?

A: 

Zed should have made an answer out of his comment. If he does, give him the upvote.

The answer is: no, you can't do that.

The function parameter (5==7) is evaluated, and the result of that evaluation is what makes it into whatever()'s scope.

I must say that I'm very curious about why you'd want to do this. Usually when I see something weird, I start thinking "that's probably the result of bad design" -- in this case, it feels more like some kind of seductive insanity ... do tell.

timdev
So ok, I'm using simpletest and I'm finding unit testing to be pretty sweet. BUT, I'm not liking how simpletest handles things (warnings and notices have mysteriously gone missing, the whole class extending UnitTestCase is used as a single testcase, when really I want each function to be its own test case, etc). SO, i'm rolling my own much simpler unit testing thingy.Long story short, I want to be able to do: assert(89 > 90) and have it print something like "You clearly suck at programming because 89 > 90 is false" . Wouldn't that be nice? I'm gonna use Josh Davis's idea.
B T
A: 

Reflection won't help you there, but you might be able to cook something up using debug_backtrace()

function funky_reflection_function()
{
    $trace = debug_backtrace();
    $file = file($trace[1]['file']);
    return $file[$trace[1]['line'] - 1];
}

Obviously, this is very hacky and shouldn't really be relied upon but if you're trying to debug stuff then it might help you achieve that. That function will give you the whole line, now you have to parse it to find what was the expression used to call your whatever() function.

Josh Davis
I thought of this right after I posted. I don't really like it cause if you something where the end paren of the function is on a separate line.. well .. you're screwed (unless you like parsing php). In any case, I think this is probably as close as you can get to what I want.
B T
A: 

As already replied, any arguments are evaluated before passing to the function, so there is no 'original code' passed.

Now that you ask for 'crazy hacks', there are, a couple of alternative ways to pass random "code" as parameter:

  • as a string - that could be used in create_function() or eval():

    somefunc ("echo 5==7;" )

  • as an anonymous function - starting at php 5.3, we have closures and anonymous functions:

    somefunc( function() { return 5==7; } );

As tim said, some more "context" information would be indeed helpful as i admit crazy hacks aren't exactly the result of good design :)

jcinacio
A: 

Just to let you know, this is the function i'm using now:

// gets the line number, class, parent function, file name, and the actual lines of the call
// this isn't perfect (it'll break in certain cases if the string $functionName appears somewhere in the arguments of the function call)
public static function getFunctionCallLines($fileName, $functionName, $lineNumber)
{   $file = file($fileName);

    $lines = array();
    for($n=0; true; $n++)
    { $lines[] = $file[$lineNumber - 1 - $n];
     if(substr_count($file[$lineNumber - 1 - $n], $functionName) != 0)
     { return array_reverse($lines);
     }
     if($lineNumber - $n < 0)
     { return array(); // something went wrong if this is being returned (the functionName wasn't found above - means you didn't get the function name right)
     }
    }
}
B T