Are you sick of doing var dumps on browser screens or error logs when you need to debug some errors? Have you seen the .Net guys step through their code line by line to figure out what went wrong? With PHP, you can do the same. Here’s how I do it.
The IDE I use is Netbeans (6.8 on Mac). It’s shipped with a built-in debugger, which works with Xdebug.
So … we first need to make sure our PHP is running Xdebug instead of Zend. I have explained how I have Xdebug setup on my XAMPP stack on Mac from one of my previous posts, Setup phpUnderControl on Snow Leopard. We can verify whether Xdebug is setup properly by loading up our phpinfo page. We should see something like this (notice the last line prints Xdebug)
Now, let’s go to Netbeans. Netbeans out of the box listens on port 9000, which is the exact port number we set our Xdebug remote_port to in our previous step. This means if we are not running our application with mod_rewrite, we now should be able to set a breakpoint in our code, hit the debug icon, and have everything going.
But … there’s always a but. When we develop Zend Framework based applications, in fact any applications that utilising mod_rewrite. We will run into trouble. We’ll find the debugger always hits the index controller, index action. To overcome this, we need to do the following.
Go to the Zend Framework application’s project property in Netbeans. Go to the “Run Configuration” option. Click on Advanced. For “Debug URL”, choose “Ask Every Time”.
Now, if we want to debug the email action from the index controller, when we hit the debug icon in Netbeans, it’ll pop up a dialog box asking us to type in the debug URL.
Once we Ok the dialog, Firefox will fire up (after you install the Netbeans debugger), and off you go!
The screenshots above are grabbed from my ZFWithDoctrine project on github.
That’s it, and hope it helps.