Made of Everything You're Not

Personal blog of PHP programmer Eric Lamb.
  • Blog
  • Portfolio
« Eclipse for PHP Dev: One Month In
Buying Books Just Got Harder »

Keeping an Eye on Your Development

Oh, var_dump()... Poor, pathetic, little vardump()... There was a time when you were my favorite little go-to for debugging. Once upon a time, you were the end all be all of problem solving tools; this would be during my idiot phase of life. Not anymore though; thanks to some god (or whatever) someone smarter than me (and apparently with more time on their hands) went and invented the Debug Toolbar.

Keeping an Eye on Your Development

In case you didn't know, a Debug Toolbar (I don't know if that's the universal name for it but it makes sense to me) is a nifty little widget that allows for easy access to all sorts of info on your web program. Since I work primarily in php that's what I'm going focus on; I'm sure they're available in other languages. Like most things; Google is your friend on this.

Usually, a Debug Toolbar contains information on included files, any sessions and cookies available, the database calls with timers and a breakdown of memory usage and performance.

Debug Toolbars usually work by encapsulating different code blocks or functions around "timer" functions to record the time it took for a block to run. All the Debug Toolbars I've used also included a memory component; really helps to see where the spikes are. There's also the database integration; it's just good sense to know where the bottlenecks are in a project.

As I said in the intro var_dump() is a handy little tool for debugging issues in php scripts. It works pretty well for small scripts but, the thing is, on larger projects it just becomes unpractical. Not only is it limited to arrays and objects but you have to manually place the call in the code and depending on the issue, you can get a HUGE amount of data back in a horrible tangle of strings. Makes XML look good.

Instead, using a Debug Toolbar, you get a nicely formatted unobtrusive way to keep an eye on how your code is performing while creating it. Needless to say, these are handy little tools.

PHP_Debug

A little disclosure: My first experience with a debug toolbar was with a really early, think pre alpha, version of PHP_Debug. PHP_Debug, at that time, worked by displaying the debug information at the bottom of a page in a table. This was definitely a step up from using var_dump(), to be sure, but it was still a little painful to decipher. Clients would complain.

Then PHP_Debug pretty much innovated the Microsoft way and copied a feature from another application; Symfony. (I kid, I kid.)

Seriously though, PHP_Debug does claim inspiration from the Symfony which I think is pretty classy of them; it seems too many times credit goes unsaid.

Keep in mind, this was the first time I saw anything like a debug toolbar. I don't want to break down to hyperbole but to say the sky parted and angels sang would be a fucking understatement.

PHP_Debug Toolbar

PHP_Debug is perfect for those stand alone projects that are started from scratch. It's not impossible to integrate PHP_Debug into an existing program, I've done it a few times, but unless you've abstracted out a good deal of the logic it's going to be a little painful. Put aside a few hours if you're going to attempt it.

The script displays the output in 2 different ways; through a floating div at the top of a page or as an HTML table displayed at the bottom of the page. Installation is pretty easy and straightforward, either as a PEAR module or as a stand alone script.

ZFDebug

ZFDebug is a plugin for the Zend Framework that acts pretty much like PHP_Debug except with a simple and easy integration into the Zend Framework and it's quite a bit slicker. Smooth animations, version details, sticky states and full of detail. If you use the Zend Framework I can't recommend it highly enough.

ZFDebug

There are some installation instructions which at the time of this writing are just broken though. Follow the linked instructions but use the below code in place of the code provided in the instructions:

protected function _initZFDebug()
{
    $autoloader = Zend_Loader_Autoloader::getInstance();
    $autoloader->registerNamespace('ZFDebug');
 
    $options = array(
        'plugins' => array('Variables',
                           'File' => array('base_path' => '/path/to/project/'),
                           'Memory',
                           'Time',
                           'Registry',
                           'Exception')
    );
 
    # Instantiate the database adapter and setup the plugin.
    # Alternatively just add the plugin like above and rely on the autodiscovery feature.
    if ($this->hasPluginResource('db')) {
        $this->bootstrap('db');
        $db = $this->getPluginResource('db')->getDbAdapter();
        $options = $db;
    }
 
    # Setup the cache plugin
    if ($this->hasPluginResource('cache')) {
        $this->bootstrap('cache');
        $cache = $this->getPluginResource('cache')->getDbAdapter();
        $options = $cache->getBackend();
    }
 
    $debug = new ZFDebug_Controller_Plugin_Debug($options);
 
    $this->bootstrap('frontController');
    $frontController = $this->getResource('frontController');
    $frontController->registerPlugin($debug);
}

Using ZFDebug is as simple as that. Once installed you'll have a div laying at the bottom of the page with all the details you could want. Plus, it comes preinstalled with the hooks for database and cache profiling. It's still possible to manually insert timing blocks but the hard part is already done for you.

Symfony

If you're using Symfony you're in luck (and none of this is probably news to you); Symfony comes with a Debug Toolbar (the original maybe?) preinstalled and read to use. All you have to do is either hit up frontend_dev.php in a browser or change:

$configuration = ProjectConfiguration::getApplicationConfiguration('frontend', 'prod', false);

to

$configuration = ProjectConfiguration::getApplicationConfiguration('frontend', 'dev', false);

in your bootstrap file.

Symfony Debug Toolbar

It's not as robust as PHP_Debug or ZFDebug out of the box (oddly, there's no included file reference) but it does allow for customization so it has the potential to be very informative and useful.

One last thing about Web Debug Toolbars; they aren't a replacement for good practices. You can't rely on them too much and sometimes your instinct will contradict the output of the toolbar. In my experience you should trust that instinct. A Web Debug Toolbar should only be used to provide insight not replace common sense.

One thing to keep in mind is to not focus too much on the

Related Posts

ACM Interactions
Stand Alone ExpressionEngine Authentication
MSRC
Importing Legacy Users Into ExpressionEngine
Nesting Platform

Tags: debug php web development

This entry was written by Eric Lamb and posted on September 07th, 2009 at 6:00 am and is filed under Code, Programming. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response below.

4 Comments

  1. รถมือสอ& says:
    September 10, 2010 at 01:38 pm

    Accetta male fatturato.

    Reply | Quote
  2. รถมือสอ& says:
    September 11, 2010 at 01:08 am

    Sie sagen gerade.

    Reply | Quote
  3. diesel says:
    September 12, 2010 at 11:46 am

    Credo che si sono errati. Io propongo di discuterne. Scrivere a me in PM, parlare.

    Reply | Quote
  4. Teddy Coll says:
    April 30, 2012 at 06:21 pm

    Hello there, just became alert to your blog through Google, and found that it is really informative. I’m gonna watch out for brussels. I will appreciate if you continue this in future. Numerous people will be benefited from your writing. Cheers!

    Reply | Quote

Leave a Reply

Click here to cancel reply.

  • Subscribe: Entries | Comments
  • About Me

    Email Email
    Twitter Twitter
    310.739.3322
  • Categories

    • Brain Dump
    • Business
    • Code
    • IT
    • Programming
    • Rant
    • Servers
  • Archives

    • February 2012
    • October 2011
    • August 2011
    • July 2011
    • June 2011
    • May 2011
    • April 2011
    • March 2011
    • February 2011
    • January 2011
    • December 2010
    • November 2010
    • October 2010
    • September 2010
    • August 2010
    • July 2010
    • June 2010
    • May 2010
    • April 2010
    • March 2010
    • February 2010
    • January 2010
    • December 2009
    • November 2009
    • October 2009
    • September 2009
    • August 2009
    • July 2009
    • June 2009
    • May 2009
    • April 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
  • Advertisement

Copyright © 2008 - 2013 Eric Lamb - All rights reserved