Get backtrace in PHP

The inevitable has occurred! PHP developers added possibility to get backtrace! There are two functions: debug_backtrace and debug_print_backtrace. The first one returns an associative array each element of which contains following structure:
array(4) {
["file"] => string(10) "<file name>"
["line"] => int(10) "<line number>"
["function"] => string(6) "<function name>"
["args"]=>
array(1) {
<list of arguments passed to the function>
}

The second function prints the backtrace in Java manner. debug_backtrace is available since PHP 4.3.0 and debug_print_backtrace – PHP 5.
In my case those functions can help me to find which SugarCRM functions cause PearDatabse errors, for example. I use the similar approach in Perl a long time. Perl includes core function caller which returns a three elemets array contained full path name, file name and line number where specific subrouting was called from. To get a full backtrace I use Carp::confess which stop the programm and print the backtrace.

Published by

Michael Stepanov

Site owner and admin :)

Leave a Reply

Your email address will not be published. Required fields are marked *