Debugging PHP

In the absence of an IDE (and particularly in a complex OO application like Dokuwiki) it's useful to be able to establish how you reached a particular point in the code. You can do this by acquiring a stack trace of the current call stack:

ob_start();
var_dump(debug_backtrace());
$result = ob_get_clean();

You can then output $result to the browser or to a log file.

Similarly when catching an exception:

try {
 
  // do something that causes an exception
 
}
catch (Exception $e) {
  ob_start();
  var_dump($e->getTrace());
  $result = ob_get_clean();
 
  // ... then report $result somehow, e.g. by writing to a log file
 
}

To write it to the Dokuwiki log file:

  1. set config option allowdebug;
  2. write $result to data/cache/debug.log (see Debug Tools) with:

    dbglog($result);