No matter your color, sex, nationality, political leanings, religious affiliation, favorite football team, or whether you think cats are make better pets than dogs, I think we can all agree on the following two points:
The suckiness factor found in #2 is compounded when building Web applications, because of the sheer number of moving parts. Although the Zend Framework no doubt streamlines the development process, debugging can still be a chore due to the data moving between the models, views, controllers, view helpers, and configuration file. Couple the added complexity with a general tendency for many, if not most PHP developers to rely on old-school debugging approaches such as using var_dump() and print_r(), and we're looking at a suck factor x 10.
If you're sick and tired of doing things the old way, it's time to change. No matter how you silently responded to the quip regarding cats and dogs, remember that both old dogs and cats can indeed learn new tricks. In this tutorial I'll show you an amazingly efficient way to debug your code using a tool called FirePHP. Even better, with the Zend Framework 1.6 release offering native support for FirePHP, configuring FirePHP for use debugging Zend Framework-driven applications is done in mere moments. Let's begin the configuration process now.
FirePHP relies on another pretty amazing Firefox add-on called Firebug. Firebug is an indispensable tool which gives developers the ability to debug, edit, and monitor CSS, HTML, and JavaScript code as it appears within the browser. If you don't already have it installed, head over to its' installation page now. Once installed, restart Firefox and continue with this tutorial.
With Firebug successfully installed, it's time to install FirePHP. To do so, fire up your Firefox browser and navigate to FirePHP's installation page. Once installed, restart Firefox and continue with this tutorial.
Zend_Log is the Zend Framework's logging component, capable of writing log messages to a variety of outputs, among them databases, e-mail, and Firebug via another component named Zend_Wildfire. Logging a message to the Firebug window is easy; just invoke the Zend_Log class, identify the Zend_Log_Writer_Firebug class as being the desigated log output, and specify your message. Here's a simple example:
$log = new Zend_Log();
$output = new Zend_Log_Writer_Firebug();
$log->addWriter($output);
$log->log('Maybe debugging is not so bad after all.', Zend_Log::INFO);
Execute the action where this code is embedded, and you'll see the log message output to the Firebug console window (which you can open by pressing F12 when Firefox is open and in focus. I prefer to open my console in a separate window, which you can do by pressing Ctrl + F12):

If you don't see any output in the console window, make sure that Firebug's Console and Net panels are enabled. If you're new to Firebug, figuring out exactly how to enable these panels can be a bit tricky. See the below figure for an example of how it's done:

Several error reporting levels are at your disposal, which will change the visual format of the log message. These levels include:
Zend_Log::EMERG: The application is offlineZend_Log::ALERT: Application is unstableZend_Log::CRIT: Very serious error conditionZend_Log::ERR: Error conditionZend_Log::WARN: Warning conditionZend_Log::NOTICE: Nothing necessarily wrong, but a worthy issue has occurredZend_Log::INFO: Informational messagesZend_Log::DEBUG: Debugging messageFor instance, logging the previous message using Zend_Log::EMERG produces the following output:

I like to use FirePHP to conveniently review complex data structures such as dynamic multidimensional arrays generated from a database query:
$supplementMap = array(
array(1,2),
array(4,2)
);
$log = new Zend_Log();
$output = new Zend_Log_Writer_Firebug();
$log->addWriter($output);
$log->log($supplementMap, Zend_Log::DEBUG);
This produces the output shown below:

Because these log messages are output no matter the environment, it would be possible for any user to install Firebug and FirePHP, and then inspect your log messages, which could lead to significant security issues. Therefore you definitely only want these messages to log when you're working in the Zend Framework's development mode. While there are several possible ways to do this, I find the easiest way to do this is to create a variable in the configuration file called debug and then check that variable's setting at run-time:
$bootstrap = $this->getInvokeArg('bootstrap');
$configArray = $bootstrap->getOptions();
$this->config = new Zend_Config($configArray);
...
if ($this->config->debug) {
$log = new Zend_Log();
$output = new Zend_Log_Writer_Firebug();
$log->addWriter($output);
$log->log("Log some debug messages", Zend_Log::DEBUG);
}
Some time ago a proposal was submitted which would make it even easier to specify the log output location according to the application's lifecycle stage setting. However it seems no progress has been made on this feature just yet.
"Easy PHP Websites with the Zend Framework" is the popular introduction to the Zend Framework, covering topics including form validation, the awesome Zend_Db database component, managing users with Zend_Auth, and much more. Click here to learn more about the book and the five hours of companion video you'll be able to access with each purchase!
Nobody has commented on this blog post yet. Why don't you be the first?
Only registered users can ask questions and post comments. If you're already registered login now. Don't have an account? Registering only takes a moment and it's free! Register now.
Have a question? Contact us!
News submitted by and about the PHP and Web development community.
Creating a Weather Forecast with PHP and the Google Weather API
Submitted by wjgilmore
How to Create an Image Gallery Powered By PHP and Picasa
Submitted by wjgilmore
Easy Databasing with SQLite
Submitted by wjgilmore
PHP IDE Roundup
Submitted by wjgilmore
Five Cool PHP Array Functions
Submitted by wjgilmore
Use jQuery with Greasemonkey
Submitted by wjgilmore
Encapsulation in PHP
Submitted by wjgilmore
Talking to Google Charts with PHP
Submitted by wjgilmore
Talking to Google Analytics with PHP
Submitted by wjgilmore
Building a jQuery/PHP-powered Chat Room
Submitted by wjgilmore
Cloud Computing with PHP
Submitted by wjgilmore
BattleMaster: A Web-based game written in PHP
Submitted by wjgilmore
Create an Image Rotator with CSS and jQuery
Submitted by wjgilmore
Copyright © 2009 W.J. Gilmore, LLC. All Rights Reserved. Purchase Policy · Privacy Policy
Trademarked names may appear on this website. Rather than use a trademark symbol with every occurrence
of a trademarked name, we use the names only in an editorial fashion and to no benefit of the trademark
owner, with no intention of infringement of the trademark.