Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get variables in scope at each PHP backtrace level?

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.

like image 860
Ian Wetherbee Avatar asked Aug 05 '10 19:08

Ian Wetherbee


People also ask

How do I get PHP to produce a Backtrace upon error?

For more advanced solution, you can use XDebug extension for PHP. By default when XDebug is loaded, it should show you automatically the backtrace in case of any fatal error. Or you trace into file (xdebug. auto_trace) to have a very big backtrace of the whole request or do the profiling (xdebug.

How can access global variable inside function in PHP?

Accessing global variable inside function: The ways to access the global variable inside functions are: Using global keyword. Using array GLOBALS[var_name]: It stores all global variables in an array called $GLOBALS[var_name]. Var_name is the name of the variable.

How can access local variable outside function in PHP?

No, you cannot access the local variable of a function from another function, without passing it as an argument. You can use global variables for this, but then the variable wouldn't remain local.

How do you define a variable accessible in functions of a PHP script?

To access the global variable within a function, use the GLOBAL keyword before the variable. However, these variables can be directly accessed or used outside the function without any keyword. Therefore there is no need to use any keyword to access a global variable outside the function.


1 Answers

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.

like image 157
kaiser Avatar answered Oct 12 '22 02:10

kaiser