The X-debug Extension

If you program at all, you understand the necessity of debugging. Fortunately, depending on the language in which you program, you can find an assortment of tools to help you with this task. One very good debugging tool for PHP is the X-debug extension. This seven-part series will reveal its capabilities and help you add it to your programmer’s toolbox.

Introduction

Without a doubt, debugging is one of the most important tasks that programmers have to tackle when developing both desktop and web-based applications. With PHP, this process can be accomplished more quickly because it’s an interpreted language.

Even so, PHP itself contains a few simple mechanisms that permit you to debug programs at a basic level. For instance, by enabling the “error_reporting” and “display_errors” directives included within its php.ini file, and also by combining “echo” statements along with the popular “var_dump()” function, it’s possible to debug runtime errors with relative ease.

However, there are certain situations that require a more sophisticated debugging process. In cases like these, it’s often necessary to verify other aspects of an application, aside from checking to see if a function is called in the correct sequence, or if a class actually produces the expected output.

On these specific occasions, the ability to evaluate the status of certain variables, measure the time that a script takes to run, and even determine how and when a given class calls its different methods is a must. Naturally, the simplest way to perform a debugging process as detailed as this is to use a third-party library, which can be picked up from the great variety available nowadays.

Even though there are plenty of debugging libraries that can be used in a pretty straightforward fashion with excellent results, there is one useful extension, called “X-debug” (http://x-debug.org) that combines good functionality, an easy learning curve and minimal installation requisites.

So, in this series of articles I’ll be providing you with a brief overview of the main features that come bundled with the X-debug extension, so you can start using it quickly for debugging your own PHP applications with minor hassles.

Now, with the preliminaries out of our way, it’s time to begin exploring the real capabilities of the X-debug package. Let’s jump in!

{mospagebreak title=Getting started using the X-debug library}

Naturally, before you start using the X-debug library, it’s necessary to install it on your own testing web server. To do so, you should navigate to its official web site, located at http://x-debug.org, and then proceed to download either the Windows or Linux module. For Windows, it’s provided as a .DLL file.

In this case, I’ll be using the library that works with PHP 5.2.1 – 5.2.6 for Windows. Once you’ve downloaded the correct file, place it in any directory of your choice (mine is “ext” under the PHP folder) and finally add the following line to your php.ini file within the “dynamic modules” section:


zend_extension_ts="c:/php/ext/php_xdebug-4.4.1-2.0.2.dll"


Please make sure that the above line includes the full path to the X-debug extension, so it can be found by the PHP engine.

Assuming that the X-debug PHP extension has been installed correctly on the web server, it’s time to test it and see if it’s really working as expected. To do this, I’m going to build a basic sample class, called “FileReader,” that looks like this:


class FileReader{

private $path;

public function __construct($path){

$this->path=$path;

}

public function read(){

if(!$content=file_get_contents($this->path)){

throw new Exception(‘Error reading target file.’);

}

return $contents;

}

}


As shown above, the previous “FileReader” class is useful for fetching the contents of a selected file, which can be directly echoed to the browser via its “read()” method. So far, there is nothing interesting happening here, right? 

This is about to change. Now I’m going to create a simple script that uses this class and passes into it an invalid path for the file to be read. Here it is:


try{

$fileReader=new fileReader(‘invalid_path/data.txt’);

echo ‘Contents of target file are the following .’.$fileReader->read();

}

catch(Exception $e){

echo $e->getMessage();

exit();

}


If all goes well (or wrong, to be more precise), as soon as the previous script is executed, the X-debug library will swing into action, and display the following error message:



At this point, you’ll have to agree with me that things are getting pretty exciting now. As shown before, the X-debug extension provides plenty of information about the error raised by the previous script, informing you not only about the nature of the error itself, but also about a few other useful debugging parameters, including the stack of calls that has been made during the script’s execution, the time it took to process each of these calls and the memory used, and finally the function and the location of each stack entry.

So at this point, you can see that the X-debug extension is up and running in the web server, and naturally it can be used for debugging PHP applications by means of additional data that isn’t available by default with the PHP interpreter.

So far, so good. At this stage I’ve demonstrated, through an introductory hands-on example, how to perform a basic test of the capabilities offered by the X-debug extension for debugging PHP applications in a detailed way.

However, this is only the beginning of this hopefully educational journey. The X-debug library comes equipped with many other handy features that deserve a close analysis. Thus, it’s time to continue exploring them in depth.

In the section to come, I’ll be showing you how to use the library’s “xdebug_call_file()” function to keep track of the location of the files included and executed by a PHP application. So, please click on the link that appears below and keep reading.

{mospagebreak title=Keeping track of file calls with the X-debug extension}

One of the most basic features that any decent PHP debugging library must have is the ability to keep track of which files are used by a script or application, including files that are loaded via “include” directives.

Fortunately, the X-debug extension provides this information in an easy-to-read format via its “xdebug_call_file().” As its name suggests, it returns the file name of the file being called for a PHP script.

Although the functionality of this function is pretty simple to grasp, the best way to understand how it works is by setting up a concrete example. With this idea in mind, I’m going to define a simple class, which will implement one of its methods with the aforementioned “xdebug_call_file()” function.

Here is how this sample class looks:


class User{

private $firstName;

private $lastName;

private $email;

public function __construct($firstName,$lastName,$email){

if(!$firstName){

throw new Exception(‘Invalid parameter First Name.’);

}

if(!$lastName){

throw new Exception(‘Invalid parameter Last Name.’);

}

if(!$email){

throw new Exception(‘Invalid parameter Email.’);

}

$this->firstName=$firstName;

$this->lastName=$lastName;

$this->email=$email;

}

// get first name

public function getFirstName(){

return $this->firstName;

}

// get last name

public function getLastName(){

return $this->lastName;

}

// get email

public function getEmail(){

return $this->email;

}

// get file from which this method was called

public function getFile(){

return ‘Called from file ‘.xdebug_call_file();

}

}


Certainly, I’m not going to waste your time explaining how the above “User” class works, since this would be pretty pointless. You should, however, pay attention to the implementation of its “getFile()” method. This method internally uses the “xdebug_call_file()” function of the X-debug extension to return to client code the name of the file being called by the current script.

This shouldn’t be difficult to grasp, but to dissipate any possible doubts, please take a look at the following code sample. It shows the output generated by the “xdebug_call_file()” function. Here it is:


try{

$user=new User(‘John’,’Doe’,’john@domain.com’);

echo $user->getFile();

 

/* displays the following

Called from file C:pathtofiledebug_call_file_example.php

*/

 

}

catch(Exception $e){

echo $e->getMessage();

exit();

}


As shown above, the “xdebug_call_file()” function returns the name of the file from which the “getFile()” method was called, which makes it really easy to keep track of the different files that are used by a PHP script. In this case, I decided to keep the previous example as simple as possible, to help you understand more easily how this function works in a concrete situation.

At this point, I’m sure that you grasped how to utilize the prior “xdebug_call_file()” function to determine from which file a function, method or statement has been called when running a PHP script. Nonetheless, the X-debug extension offers another function, called “xdebug_call_line(),” which can be used for determining from which line an instruction has been invoked.

In the upcoming segment, I’m going to illustrate how to use this brand new debugging function. Please click the link that appears below and read the next few lines.

{mospagebreak title=Working with the xdebug_call_line() function}

As I mentioned in the previous section, the X-debug extension includes another useful function, called “xdebug_call_line(),” which returns the line number from which a method, function or statement has been invoked.

To demonstrate how this function works, I’m going to modify the signature of the sample “User” class that you learned before, which now will use the “xdebug_call_line()” function for implementing a brand new method, named “getLine().”

That being said, here’s the modified signature of the “Users” class:


class User{

private $firstName;

private $lastName;

private $email;

public function __construct($firstName,$lastName,$email){

if(!$firstName){

throw new Exception(‘Invalid parameter First Name.’);

}

if(!$lastName){

throw new Exception(‘Invalid parameter Last Name.’);

}

if(!$email){

throw new Exception(‘Invalid parameter Email.’);

}

$this->firstName=$firstName;

$this->lastName=$lastName;

$this->email=$email;

}

// get first name

public function getFirstName(){

return $this->firstName;

}

// get last name

public function getLastName(){

return $this->firstName;

}

// get email

public function getEmail(){

return $this->email;

}

// get line from which this method was called

public function getLine(){

return ‘Called from line ‘.xdebug_call_line();

}

}


Here, it’s clear to see how the above “getLine()” method defined within the  “User” class uses the “xdebug_call_line()” function to return to client code the line from which the method in question has been called.

Does this sound rather confusing? It’s not, really. The sample script shown below should give you a clear idea of how the “xdebug_call_line()” function works. Here it is:


try{

$user=new User(‘John’,’Doe’,’john@domain.com’);

echo $user->getLine();

 

/* displays the following

Called from line 40

*/

}

catch(Exception $e){

echo $e->getMessage();

exit();

}


See how simple it is to utilize the “xdebug_call_line()” function within a sample class? I hope you do! In this specific case, the function returns the line from which the “getLine()” method has been called. Simple and useful too.

Of course, this function may seem a bit primitive to be used for debugging purposes. But, when combined with the “xdebug_file_call()” function that you saw before, you can get more detailed information on the way that your script works. Try them out for yourself and see how they behave in each case.

Final thoughts

In this first chapter of the series, I provided you with a quick introduction to installing and using the X-debug PHP extension for debugging a couple of simple PHP scripts. As I said in the introduction, this extension is pretty simple to configure and use, and these features make it suitable for both beginners and advanced programmers.

In the upcoming article, I’ll be explaining how to use X-debug for keeping track of the functions being called during the execution of a PHP script.

Now that you know the topic for the next tutorial, don’t miss it!

[gp-comments width="770" linklove="off" ]

chat sex hikayeleri Ensest hikaye