Debugging PHP

As an old fashioned programmer I grew up with debugging methods like post-mortem traces and trace statements.

Today however we have and are used to GUI’s for debugging and can single step code or even re-compile code and retry the operation. This is all nice in environments where applications can be frozen. If not, like web pages and applications depending on real-time communication with devices the single stepping alone ruins the applications workings and thus the debugging process.

Here old fashioned trace message and a viewer for them come in handy again. Normally on Microsoft Windows one uses the OutputDebugString() API. For PHP this API call was missing so I implemented a simple PHP extension that wrapped the API in two ways. One is just the call and the other way is as member function of an object. 

Big advantage of the OutputDebugString() API is that if there is no viewer active, the output is just ignored and vanishes into thin air, leaving no traces like massive log files. Also good to know is that it’s impossible to ruin http headers etc as the output is redirected to something else then the web browser.

The extension was written in Borland Delphi using the easy to use Php4Delphi library. As viewer one can use the free DbgView from Sysinternals.

The result is a very easy to use extension that can be left in the code for as long as one want/needs.

The following snippet test of the module is indeed loaded properly by the PHP interpreter:

   1: $module = "log";
   2:  
   3: if(!extension_loaded($module)) {
   4:   echo "Log Module not Loaded";
   5:   exit;
   6: }

 

This snippet uses the php_log class:

   1: $log = new php_log();
   2: $log->cleardebugwindow();
   3: $log->outputdebugstring("PHP test log class", $log->info);

 

The cleardebugwindow() method send a special message to DbgView clearing the display. Outputdebugstring() takes two parameters, the message and a severity string. This last parameter is handy for grouping the messages or be able to search on certain types. It is not necessary to use the built-in types like info, warning or error, any tag is allowed.

The following code is not using classes:

   1: outputdebugstring("PHP test module", 'error');

 

The sources can be downloaded from this link. In order to compile it, you’ll also need to download php4delphi and configure it correctly for your PHP version and off-course a Borland Delphi version.

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s