views:

2136

answers:

6

I'm looking for a way to print the call stack in PHP.

Bonus points if the function flushes the IO buffer.

+12  A: 

Hi,

If you want to generate a backtrace, you are looking for debug_backtrace and/or debug_print_backtrace.


The first one will, for instance, get you an array like this one (quoting the manual) :

array(2) {
[0]=>
array(4) {
    ["file"] => string(10) "/tmp/a.php"
    ["line"] => int(10)
    ["function"] => string(6) "a_test"
    ["args"]=>
    array(1) {
      [0] => &string(6) "friend"
    }
}
[1]=>
array(4) {
    ["file"] => string(10) "/tmp/b.php"
    ["line"] => int(2)
    ["args"] =>
    array(1) {
      [0] => string(10) "/tmp/a.php"
    }
    ["function"] => string(12) "include_once"
  }
}


They will apparently not flush the I/O buffer, but you can do that yourself, with flush and/or ob_flush.

(see the manual page of the first one to find out why the "and/or" ;-) )

Pascal MARTIN
+2  A: 

See debug_print_backtrace. I guess you can call flush afterwards if you want.

Martin Geisler
+1  A: 

debug_backtrace()

pix0r
+1  A: 

You might want to look into debug_backtrace, or perhaps debug_print_backtrace.

Rob
+1  A: 

Use debug_backtrace to get a backtrace of what functions and methods had been called and what files had been included that led to the point where debug_backtrace has been called.

Gumbo
+3  A: 
var_dump(debug_backtrace());

Does that do what you want?

inkedmn