Archive for the ‘ Debugging Techniques ’ Category

Debugging PHP: the basics

One of the core development skills is debugging: the process of identifying why output and expected output are not the same. Most developers I’ve met are lousy debuggers. This article gives a few hints and tips.

Use a debugger: stepping through the code line by line watching the variables change makes debugging a joy. An added benefit is that you will undestand your application better by watching how variables change in methods and loops: in a loosly typed language you are never fully sure exactly what’s stored in a variable of what a method returns.

Break done the application into chunks. Split into two section (using breakpoints or die() if you’re not using a debugger). Is the output of the first section what you expect? If so the error is in the second section. Split the offending section into two and continue until you find the exact line with the problem.

If you can’t find an error – is your ecpected output correct? Have you misunderstood the business problem? If so you need to work out what the output should be at each stage and go back through the process of breaking down the application into chunks.

Let the debugger finish. It’s amazing what happens if you let class destructors run. One of my developers was completetly confused by duplicate database entries. He’d sat for hours with debugger and only ever saw one database insert happenning. But each time he ran the debugger he’d complete application logic and stop the debugger. It wasn’t until we let the application finish we saw the destructors fire and insert the duplicate row into the database.

Add unit tests. If a particular use case is causing a problem add a test to the test suite for it. If you haven’t got a test suite now is the time to start one. Next time the code is changed you’ll know wether or not the bug will reappear. The one thing that users, and project managers, hate more than any other is bugs that have been fixed reappearing.

Using a debugger with a web application is hard. Zend Studio allows you to debug a web page. And it works – just. If you are using the MVC pattern make sure that the controllers are essentially trivial; the business logic is where it belongs in the models; and your views are preparing HTML. This means that an application error is most likely to be in the models. You should be able to run the models through the debugger by using a test harness, for example by creating unit tests, sepeartely from the controllers. It’s much, much easier to use a debugger on a model than it is a controller. A display error will be in a view – it should be very simple to identify which view and to correct the problem. To debug a controller connect it to a test stub rather than the real model so you know what data is going in and how it should be presented to the model; the logic in a controller should be so simple that this is rarely necessary.

Get familiar with PHPUnit and test driven development. You’ll find you spend much less time debugging.