Debugging PHP w/ FirePHP

Firebug / FirePHPI’ve been using FirePHP for a couple of years. As a PHP developer, it makes my job so much easier. At virtually any point in my code I can check the value(s) of variables, arrays or objects without using primitive debugging methods. Instead of using print_r, var_dump, or logging to a file, I just use FirePHP to send whatever I want to see to the Firebug console. One of the best parts about using FirePHP is that logging to the console doesn’t mess with the natural execution of the code, and the display on-screen is just as it would be if I wasn’t using FirePHP. I’ve used FirePHP with CodeIgniter, Slim, Laravel and WordPress, and as far as I’m concerned, there isn’t a better tool for debugging in PHP.

FirePHP for CodeIgniter

Using FirePHP in CodeIgniter is super easy. All that needs to be done is put fb.php and FirePHP.class.php in the libraries directory and add “fb” to the autoloaded libraries.

$autoload['libraries'] = array('fb');

FirePHP for WordPress

FirePHP for WordPress is a little tricky, because lets face it, WordPress is a bit of a beast. If you want to use FirePHP with WordPress, how you use it may depend on what you are debugging. In most cases, you’ll probably be able to use a simple plugin I created:

<?php
/*
Plugin Name: BWD FirePHP Debugger
Description: FirePHP PHP Debugger for WordPress
Author: R.B. Gottier
Version: 1.0
Author URI: http://brianswebdesign.com
*/

include_once 'fb.php';

ob_start();

class firephp_debugger {

  public function __construct()
  {
    // On activation
    register_activation_hook( 
      __FILE__, 
      array( $this, 'on_activation' ) 
    );

    // On update
    add_filter( 
      'pre_update_option_active_plugins', 
      array( $this,'on_update' ) 
    );
  }

  public function on_activation()
  {
    $file_name = plugin_basename( __FILE__ );
    
    $plugins = get_option('active_plugins');
    
    update_option( 
      'active_plugins', 
      $this->process( $file_name, $plugins ) 
    );
  }

  public function on_update( $plugins )
  {
    $file_name = plugin_basename( __FILE__ );
  
    if( ! in_array( $file_name, $plugins ) )
    {
      return $plugins;
    }

    return $this->process( $file_name, $plugins );
  }

  public function process( $file_name, $plugins )
  {
    $new = array();
  
    array_push( $new, $file_name );
  
    foreach( $plugins as $plugin )
    {
      if( $plugin != $file_name )
      {
        $new[] = $plugin;
      }
    }

    return $new;
  }

}

$firephp_debugger = new firephp_debugger;

I’ll admit, that’s not much of a plugin, but if you put that in your plugins directory with the fb.php and FirePHP.class.php files, you’re in business. You’ll notice that all the plugin is doing is loading the included file and starting some output buffering. Also, it’s making sure it’s the first plugin loaded, so you can use it to debug other plugins.

FirePHP for Slim or Laravel

If you’re using Slim or Laravel, I can almost guarantee that you don’t need any help getting FirePHP working. It has been a while since I used FirePHP with Laravel, but should you want to know how I used it with Slim, I’d be glad to tell you. I created a “lib” directory inside a “app” directory that lives at public root. Once I dropped db.php and FirePHP.class.php in the “lib” directory, I just put the following at the top of my index.php file:

<?php
require 'Slim/Slim.php';
\\Slim\\Slim::registerAutoloader();
$app = new \\Slim\\Slim();

include 'app/lib/fb.php';

Downloads

The fb.php and FirePHP.class.php files I reference in this blog post can be found on the download page of the FirePHP website. To make things easy for you, I’ve put a copy of the download on my server, and a second download for my FirePHP WordPress plugin (currently working in WP 3.5.1):

FirePHPCore Library v0.3.2 or FirePHP PHP Debugger for WordPress v1.0

Using FirePHP

Using FirePHP is really easy. You’ll first want to install the Firebug extension and FirePHP extension for Firefox. The FirePHP website shows lots of ways you can use it, but I’ll typically just use the log method. You can get the values of variables, arrays and objects with the log method. Here’s an example of how to have FirePHP log the values of an array to the console:

<?php
// Basic array
$arr = array(
  'one', 'two', 'three'
);

/**
 * Show the values in $arr. This is
 * the way I normally use FirePHP, and
 * this works great in WordPress:
 */
FB::log($arr);

/**
 * Because of PHP namespaces, in Slim
 * you would add a backslash like this:
 */
\\FB::log($arr);

/**
 * If you are using CodeIgniter, you
 * would usually do something like this:
 */
$this->fb->log($arr);

/**
 * The CodeIgniter example above only
 * works if you've got an instance of
 * the CI super object. If you don't, 
 * then you would do something like this:
 */
$CI = get_instance();
$CI->fb->log($arr);

I hope that using FirePHP will make you more productive. Because FirePHP works with Firebug which is a Firefox extension, it’s not for everyone. For you Google Chrome lovers out there, be sure to check out ChromePHP. Although not as nice as FirePHP in my opinion, it’s better than nothing.

Posted in PHP