PHP debugging with FirePHP

FirePHP is a PHP debugging tool that is super helpful for, well, debugging PHP and much easier than doing a lot of print_r() to the page and easier access to Ajax results, etc. You can group results, show full arrays, objects, warnings, exceptions, and apply object filters to large objects to make then less unwieldy.

The “Insight” library is more full-featured and uses the “DeveloperCompanion” Firefox extension while FirePHP, which has been around for a while now works again with Firebug. I’ve used both but haven’t really had the patience to really discover the benefits of the Insight library. I tend to just prefer to use it like one would use Firebug’s console.log and have the messages appear in the same console panel in Firefox so your JS and PHP debugging is all in one place.

Unfortunately you can’t use Insight and FirePHP extensions at the same time, so switching requires browser restart. Hopefully the Insight library will be Firebug compatible at some point. One small, but annoying thing is that the Insight method pops a window in front of your active browser window causing it to lose focus. With a big enough monitor(s) it’s not a huge issue but if you don’t need the report every reload it can be a pain for small debugging tasks, so I’ve pretty much switched back to the old FirePHP tools.

Using the OO API, the syntax is nearly the same, so after swapping FF extensions, switching the code mostly involves a different init script which could look something like this:

$method = 'firephp';
// INSIGHT
if ($method == 'insight') {
	require_once('phar:///htdocs/lib/firephp.phar/FirePHP/Init.php');
	$inspector = FirePHP::to('request');  // page | controller | request
	$console = $inspector->console(); // 'Debug'
	// ... (also, make sure "insight keys are enabled) //
} else {
// FirePHP
	require_once('phar:///htdocs/lib/firephp.phar/FirePHPCore/FirePHP.class.php');
	$console = FirePHP::getInstance(true); 
	$console->setEnabled(true); // default = true -- if using in code that may get deployed, filter by IP
}

and put that in your bootstrap file or other include. The basic syntax is:

$console->log($var);

Output looks like:

Click here to see source for debug messages shown above

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php 
echo '<pre>';
require_once 'firePHPbootstrap.php';
if ($method == 'insight') {
	$console->show();
}
####################################
$msg = 'Hello Worldx';
$time_msg = $msg .' :: ' . time() . ' ';
//
$console->log($msg); #exit;
/**/
$var = array('i'=>10, 'j'=>20);
$console->log($var);
//
try {
  throw new Exception('Exception');
} catch(Exception $e) { 
	$console->log($e);
	$console->log('line: ' . $e->getLine());
}
 
class foo {
	public $one = 'one';
	public $two = 'two';
	public $three = 'three';
}
$bar = new foo();
 
$console->log($bar);
 
/**/
if ($method == 'insight') { 
	$console->group('group1')->open();
	$console->log('Group 1');    // Group Label
	$console->log('Group 1 message 1');
	$console->group('group1')->close();
} else {
	$console->group('Group 1');    // Group Label
	$console->log('Group 1 message 1');
	$console->groupEnd();
}
//
$console->log('Log Message');
$console->info('Info message');
$console->warn('Warning message');
$console->error('Error message');
//
$table = array();
$table[] = array('Row 1  Column 1', 'Row 1 Column 2');
$table[] = array('Row 2  Column 1', 'Row 2 Column 2');
$console->table('Sample Table', $table, array('Column 1', 'Column 2'));
 
$console->log($time_msg);
?>
<hr />
<?php echo $time_msg; ?>

Leave a Reply