Search code examples
phpdebuggingdebug-backtrace

Debugging PHP Code with debug_backtrace


I love to save time using someone else's code. And I want to start effectively debugging my scripts, as well as scripts I inherit from other developers.

I've been reading up on debug_backtrace(), and I'm not sure if it's what I'm looking for.

Basically,when a class is instantiated I want to know what methods are being fired. Truthfully, I'd like to know as much as possible, but knowing what's going on inside a single class would be fantastic.

<?php
require('aHugeComplicatedClass.php'); // sooooo many methods

try {

   $obj = new aHugeComplicatedClass($params);

}
catch(Exception $ex){

   var_dump($ex);

}

?>

From PHP's docs about debug_backtrace, it looks like I need to place the debug_backtrace() function inside each method/function within any and all classes, just to see how it was reached.

I gotta be reading this too literal. That would be a ton of modifications.

So, if I have a php file, that instantiates a class, and I know this class is extended from other classes, what's the simpliest way to debug that Object?


Solution

  • I would install XDebug and hook up the remote debugging to your IDE (e.g PhpStorm or Eclipse), that way you will get nice stack dumps on all errors, plus the ability to breakpoint your code and inspect the stack and all object internals at your leisure.

    http://xdebug.org/

    You can also use it to profile your application call chains without making any code changes (which sounds more like what you are wanting). By using the profiling options, which generate big log files, you can then load these logs into webgrind and visually inspect who's calling what in nice tree structures.

    https://code.google.com/p/webgrind/

    The Zend tool chain would also provide this kind of deeper debugging functionality out of the box.

    Alternatively install an Application Performance Monitoring agent such as App Dynamics or New Relic for similar code-profiling. This is most useful for remote installations (i.e. production) where debugging isn't an option and profiling is expensive.