tags:

views:

42

answers:

3

Hey all,

I'm trying to figure out how to know what has been done to a variable.

Here's an example:

function a($hello) {

$out .= strtoupper(ucwords(strtolower($hello)));
return $out;

}

echo function_trace('$hello') // returns array(strtoupper,ucwords,strtolower)

Thanks! Matt

+5  A: 

There's not really an easy way to do this, because variables don't store "state" or "history". Stack traces (where you probably got your inspiration from) are possible because they're generated from the existing execution stack, which is stored out of necessity to be able to properly unwind chains of function calls.

In addition, your example is trying to trace a function parameter - but that parameter variable is only defined within the scope of the function. Attempting to reference it outside of the function would result in the interpreter not knowing what variable you're trying to indicate - it'd think you're looking for a globally-scoped $hello, not the one used as an argument in the function.

Amber
A: 

There's no hook in PHP that does exactly what you want, but you can get a call stack with debug_backtrace():

http://php.net/manual/en/function.debug-backtrace.php

beamrider9
It's not _exactly_ what he wants.
zneak
A: 

It's not possible to do exactly what you're asking for, but perhaps if you gave a bit more context about what you were hoping to do with that function trace, we could give some suggestions?

Sam Minnée