8

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.

Ian Wetherbee
  • 6,009
  • 1
  • 29
  • 30
  • 1
    Curious what you would need this for. – Gordon Aug 05 '10 at 19:29
  • 5
    Does `XDebug` not do what you need? Why re-invent a full blown debugger when installing one really really simple? – ircmaxell Aug 05 '10 at 19:33
  • 2
    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. – Ian Wetherbee Aug 05 '10 at 20:00
  • 1
    I can't find anything in [the docs](http://www.php.net/manual/en/ref.errorfunc.php) either, unfortunately. [Python can do it](http://docs.python.org/library/inspect.html), don't see why PHP shouldn't be able to. – mpen May 29 '12 at 18:27
  • 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. – Clox Apr 23 '15 at 18:40

2 Answers2

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.

kaiser
  • 21,817
  • 17
  • 90
  • 110
-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.

Christian South
  • 359
  • 2
  • 9