Section 22.8. Backtracing Your Code


22.8. Backtracing Your Code

Debugging complex scripts can sometimes be a nightmare because objects call functions, which call other objects and other functions, and so onyou end up with a nest of calls that make tracing the problem difficult. To make your life easier, you can use the function debug_backtrace( ) to tell you about the chain of events that led up to the call to debug_backtrace( ).

For example:

     function A($param1, $param2) {             B("bar", "baz");     }     function B($param1, $param2) {             C("baz", "wom");     }     function C($param1, $param2) {             var_dump(debug_backtrace( ));     }     A("foo", "bar"); 

That script calls function A( ), which calls B( ), which calls C( ), which var_dump( )s the output from debug_backtrace( ). Now, what debug_backtrace( ) will return is an array of the steps that occurred in getting to it, so that script should output the following:

     array(3) {             [0]=>             array(4) {                     ["file"]=>                     string(20) "C:\php\backtrace.php"                     ["line"]=>                     int(6)                     ["function"]=>                     string(1) "C"                     ["args"]=>                     array(2) {                             [0]=>                             &string(3) "baz"                             [1]=>                             &string(3) "wom"                     }             }             [1]=>             array(4) {                     ["file"]=>                     string(20) "C:\php\backtrace.php"                     ["line"]=>                     int(3)                     ["function"]=>                     string(1) "B"                     ["args"]=>                     array(2) {                             [0]=>                             &string(3) "bar"                             [1]=>                             &string(3) "baz"                     }             }             [2]=>             array(4) {                     ["file"]=>                     string(20) "C:\php\backtrace.php"                     ["line"]=>                     int(11)                     ["function"]=>                     string(1) "A"                     ["args"]=>                     array(2) {                             [0]=>                             &string(3) "foo"                             [1]=>                             &string(3) "bar"                     }             }     } 

Start from the first element, 0, and work your way down in order to visually backtrace the steps performed before debug_backtrace( ) was called. Each element in the return from debug_backtrace( ) is an array of values that together form a "step"here is how it works:

  1. The first element (step) has a "file" of c:\php\backtrace.php, which means this is where the code was at this step. "Line" is set to 6, and "function" is set to "C", which means that on line 6 of c:\php\backtrace.php, C( ) was called. There is also an "args" array containing "baz" and "wom"the two parameters passed into C( ).

  2. The second element tells us that B( ) was called on line three of the same script, with the parameters "bar" and "baz".

  3. The third element tells us that A( ) was called on line 11 of the same script, with "foo" and "bar" passed in.

That is the complete contents of the array, but you can see it has told us exactly how PHP got to where it was, including all the parameters passed into functions. This is invaluable for tracking down bugs, particularly when bad parameters are being passed into functions. Having the "file" element in each step also means that it works very nicely across multiple files, so even the most complicated scripts are brought to heel with debug_backtrace( ).



PHP in a Nutshell
Ubuntu Unleashed
ISBN: 596100671
EAN: 2147483647
Year: 2003
Pages: 249

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net