PHP Software Engineering Using a Debugger Extension - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Software Engineering Using a Debugger Extension - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript


Post Top Ad

Post Top Ad

Saturday, July 6, 2019

PHP Software Engineering Using a Debugger Extension

PHP Software Engineering

Using a Debugger Extension


You want to debug your scripts interactively during runtime.


Use the Xdebug extension. When used along with an Xdebug-capable IDE, you can examine data structure; set breakpoints; and step into, out of, or over sections of code interactively.


This recipe focuses on Xdebug’s interactive debugging capability. To follow along, you need to be able to compile and install a Zend extension, which means permissions to edit php.ini on your system. PHP’s dl() extension-loading function does not work with Xdebug. Finally, examples in this recipe are intended to work with Xdebug 2.2.

Installing the Xdebug extension is a straightforwar7+d procedure. You can build from source, or you can install using the pecl command:

       % pecl install xdebug

After you have the extension compiled and installed, you need to edit your php.ini file with the full path to the module, such as zend_extension = /usr/lib/php/extensions/no-debug-non-zts-20050922/ The directory into which pecl installs is the directory specified as the value of the extension_dir configuration directive.

You’ll know you’ve got Xdebug installed correctly when running php -m from the command line lists Xdebug twice—once in the [PHP Modules] section of the output and once in the [Zend Modules] section of the output. If you’re trying to install Xdebug with a version of PHP you access via your web browser, check for an “xdebug” section in the output of the phpinfo() function.

In addition, you need to set the xdebug.remote_enable configuration directive to on for remote debugging to function.

Installing the Xdebug extension, however, is only half of what you need for interactive debugging. The other half is a debugging client that can talk to Xdebug and help you inspect your program. In this recipe, we’ll use NetBeans IDE as an example—it’s free, cross-platform, and easy to operate. Lots and lots of IDEs, both free and commercial, support Xdebug and its DBGp debugging protocol.

To set up NetBeans to talk to your Xdebug installation, you need to do a few things. First, in your Project Properties, ensure that your “Project URL” points to the URL of your web server running PHP. Then, in the PHP section of the preferences pane make sure the Debugger Port value matches what PHP is configured to use for xdebug.remote_port (usually 9000).

If everything is set up properly, when you execute the “Debug Project” command in NetBeans, it will fire up a request to the home page of your project in your web browser, and position execution at the first PHP line in your project’s home page, displaying the code and a Variables watch pane.

Figure Beginning a debug session

In the figure, the red square replacing the line number marker on line 12 is a result of clicking the 12 and inserting a breakpoint. Hitting F5 to continue execution causes PHP to run until the breakpoint is reached.

Figure Continuing a debug session

At the breakpoint, the Variables watch pane has been updated to include the values of the local variables $a and $bob.

Xdebug supports the wide variety of features you’d expect from an interactive debugger—breakpoints, watchpoints, stack traces, and so forth. Exactly how you access them will depend on your IDE. But when your PHP program is behaving mysteriously, nothing beats being able to set a breakpoint, run exactly to a particular line, and then inspect the values of variables at that point.

No comments:

Post a Comment

Post Top Ad