Using FirePHP (FireBug) with Prestashop

Debugging can be such a pain sometimes...

I've been using FirePHP for some time now on my CodeIgniter projects and recently while I was looking at doing some cool things with the product data returned by the Category::getProducts() function it struck me that it would be nice to be able to easily see what data was returned. Why I've never used the FirePHP class in my Prestashop projects before I've no idea, as it really is very simple to use, if a little erratic in its behaviour....

Install the Firefox Plugins

First step is to install FireBug in Firefox, and then, in addition install FirePHP. Now we've got the tools installed in our browser we can look at outputting to the console from within our own code!

Install the Server Library

Next go to the FirePHP HQ and download the latest version of the FirePHPCore library. Once you've done this, open the archive and find the file FirePHP.class.php and rename it to FirePHP.php, then copy it into the /classes folder of your PrestaShop development site and you're done!

Using FirePHP in your code

For modules I've started adding a private member variable to my module class, and initialising the library within the constructor -- this makes it easy to call the debug code whenever I need it. For the MovingBoxes module the top of my source file now looks like:

class MovingBoxes extends Module
    private $_html = '';
    private $_postErrors = array();
    private $fb;
    function __construct()
        $this->name = 'movingboxes';
        $this->tab = '';
        $this->version = '0.1.0';
        $this->displayName = $this->l('Moving Boxes - Home page featured products');
        $this->description = $this->l('A simple jQuery scrollable featured products module.');
        $this->fb = FirePHP::getInstance(true);

Within my code I can then call the FirePHP API to log messages, errors warnings etc. and also output the contents of variables. In the case of the MovingBoxes module again:

        public function hookHome($params)
        global $smarty;
        $category = new Category(1);
        $nb = intval(Configuration::get($this->name.'_number'));
        $products = $category->getProducts(intval($params['cookie']->id_lang), 1, ($nb ? $nb : 10), 'date_add', 'DESC');
        $this->fb->log($products, 'The output from Category::getProducts()');
            $this->name.'_products' => $products,
            $this->name.'_path' => _MODULE_DIR_.$this->name.'/',
            $this->name.'_offer_message' => $this->l('Only')
        $this->fb->log('At end of MovingBoxes::hookHome()');
        return $this->display(__FILE__, 'movingboxes.tpl');

The only problem is that it's sometimes hard to actually catch the output in the console, and usually a combination of refresh and Ctrl-F4 is required to get it to display. It does make getting information out of PrestaShop a lot easier though! If you want to place the code in other places (not within a module) then all you need to do is get the instance, then use it to call the function i.e.

$firebug = FirePHP::getInstance(true);
$firebug->error('Display an error message in here');

A Word of Caution

Please remember to remove the class and function calls from your code before releasing it! This can expose the internals of your site and represent a serious security risk if you don't. YOU HAVE BEEN WARNED!