开发者

Find what functions were called (from a variable's perspective)

开发者 https://www.devze.com 2022-12-22 00:51 出处:网络
I\'m trying to figure out how to know what has been done to a variable. Here\'s an 开发者_如何学运维example:

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


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.


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


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号