Getting variables in scope at every level of PHP backtrace?

Is there a way to view the variables set on each stack stack in backtrace? I can get pretty close with a combination of debug_backtrace(true) to get objects, get_object_vars for each object to get $ this vars, the args key in each backtrace frame and get_defined_vars to get global values, but any temporary variables set inside the function, I can’t find a way to get it.

Here is an example of a situation:

 function method1($foo) { $temp = method2($foo + 1); foreach ($temp as $t) { method2($t); } } function method2($bar) { $temp2 = $bar->value + $_GET['val']; debug(); } function debug() { // to be created $global_scope = get_defined_vars(); $bt = debug_backtrace(true); } 

I can get $foo and $bar using the args key in backtrace, $bar object variables via get_object_vars and global variables via get_defined_vars . I want to get the value of $temp2 and $temp .

+6
scope php backtrace
source share
2 answers

Install and enable XDebug on the local server. Then use xdebug_get_declared_vars() . Make sure you set xdebug.collect_vars to On In the xdebug .ini file.

Example:

 <?php class strings { static function fix_strings($a, $b) { foreach ($b as $item) { } var_dump(xdebug_get_declared_vars()); } } strings::fix_strings(array(1,2,3), array(4,5,6)); ?> 

Return:

 array 0 => string 'a' (length=1) 1 => string 'b' (length=1) 2 => string 'item' (length=4) 

Example from xdebug.org

Note that the function returns only variables in the area where the xdebug_get_declared_vars() function is xdebug_get_declared_vars() .

+1
source share

Cancel debugging to accept 1 parameter. Then just go to get_defined_vars . This will give you an array of all the vars in the local area.

-one
source share

All Articles