Get variables in scope at each PHP backtrace level?
Asked Answered
H

2

9

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

Here's an example 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 via the args key in the backtrace, the object variables of $bar through get_object_vars, and the globals through get_defined_vars. I want to get the value of $temp2 and $temp as well.

Halstead answered 5/8, 2010 at 19:27 Comment(5)
Curious what you would need this for.Shipowner
Does XDebug not do what you need? Why re-invent a full blown debugger when installing one really really simple?Thylacine
As far as I can tell, XDebug can only do the top stack too: "With the xdebug.show_local_vars setting you can instruct Xdebug to show all variables available in the top-most stack level for a user defined function as well." Part of this is so I can quickly inspect values Firebug-style by tying the values to the highlighted source output variables. The other part is just for kicks.Halstead
I can't find anything in the docs either, unfortunately. Python can do it, don't see why PHP shouldn't be able to.Bunk
This is what I was looking for too. At least I now know it isn't possible. Unless it is now? It would be a great aid in debugging and seeing exactly where things have gone wrong and where a variable has an unexpected value.Harmonics
G
1

Install and Enable XDebug on your (local) server. Then use xdebug_get_declared_vars(). Make sure that you set xdebug.collect_vars to On in your 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));
?>

Returns:

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

Example from xdebug.org

Note, that the function only returns variables in the scope where the function xdebug_get_declared_vars() is called in.

Gunas answered 20/8, 2015 at 13:53 Comment(0)
D
-1

Alter your debug to take 1 param. Then just pass in get_defined_vars. This will give you an array of all the vars in the local scope.

Doubleminded answered 1/2, 2011 at 2:46 Comment(1)
I already used that method to get the local vars, the problem is getting the local vars in the OTHER stack frames.Halstead

© 2022 - 2024 — McMap. All rights reserved.