Debugging Program Flow with the Xdebug Extension

The Xdebug PHP extension combines a set of powerful functions with an easy learning curve. This appealing mixture turns it into the choice of many PHP developers for debugging PHP applications with a great level of detail. If you want to learn how to get the most out of this debugging library without having to spend long hours reading its user manual, then keep reading. In this seven-part series of articles you’ll find an approachable guide to utilizing its most important functions. I will use numerous code samples to instruct you in the use of the Xdebug extension.

Introduction

Of course, if you already read the previous tutorial of the series, then you’re already familiar with using a few simple, yet useful, functions provided by the Xdebug extension, such the popular “var_dump()” function. As you’ll possibly recall, the extension offers an enhanced version of this function that lets you get detailed information about one or more PHP variables.

What’s more, when used with an object, the “var_dump()” function permits you to retrieve a complete set of parameters, including the name of the properties of the targeted object, and their types and values. This feature can be pretty useful when debugging object-based PHP programs.

Nonetheless, the X-debug extension comes equipped with many other functions that allow you to perform all sorts of debugging tasks. So, provided that you’re interested in learning a bit more, in this fourth article I’ll be explaining how to use the complementary “xdebug_start_code_coverage()” and “xdebug_get_code_coverage()” functions. They can help you keep track of which lines are executed by a specified PHP program.

The usage of these functions is very interesting, trust me, so don’t waste more time in preliminaries. Start reading about them now!

{mospagebreak title=Review: getting information about PHP objects with the var_dump() function}

Before I start explaining how to use the aforementioned “xdebug_start_code_coverage()” and “xdebug_get_code_coverage()” functions, I will quickly review the practical example created in the preceding article of this series. It demonstrated how to retrieve relevant information about a specified object through the “var_dump()” function.

Basically, the entire source code of the example in question looked like this:

(example on using the var_dump() function with a sample array)


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;

}

}


try{

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

$data=array(‘string’=>’This is a string’,’integer’=>1,’float’=>0.123456,’object’=>$user);

var_dump($data);

 

/* displays the following

array

‘string’ => string ‘This is a string’ (length=16)

‘integer’ => int 1

‘float’ => float 0.123456

‘object’ =>

object(User)[1]

private ‘firstName’ => string ‘John’ (length=4)

private ‘lastName’ => string ‘Doe’ (length=3)

private ‘email’ => string ‘john@domain.com’ (length=15)

*/

}

catch(Exception $e){

echo $e->getMessage();

exit();

}


As shown above, the “var_dump()” function can be very helpful for retrieving information about a group of specified variables, since its functionality has been extended when used with the Xdebug library. In this specific case, the function has been fed with a basic array of elements, where one of them is an instance of the “User” class listed before.

Aside from retrieving the name of the object passed as an incoming argument, the function also returns a few other helpful values. These include the name of its properties and the corresponding length expressed in characters.

At this stage, and having already reviewed how the “var_dump()” function does its thing, it’s time to explore more functions that come bundled with the Xdebug extension. Thus, as I explained in the introduction, the library allows you to follow the flow of a specified application by means of the “xdebug_start_code_coverage()” and “xdebug_get_code_coverage()” functions.

Thus, in the upcoming section I’ll be coding a brand new example. It will demonstrate how to use these functions to determine which lines are executed by a PHP program.

Click on the link below and keep reading, please.

{mospagebreak title=Debugging program flow with the xdebug_start_code_coverage() and xdebug_get_code_coverage() functions}

As I anticipated in the section that you just read, the X-debug extension provides two handy functions, called “xdebug_start_code_coverage()” and “xdebug_get_code_coverage().” When used in conjunction, these functions allow us to keep track of which lines are executed by a PHP application.

Naturally, to grasp more easily how these functions work, you should take a look at the following code sample. It creates a simple loop with a “for” PHP statement, and then displays the sequence of program lines that have been executed. Here’s the code sample in question:

xdebug_start_code_coverage();

function displayIntegers(){

for($i=1;$i<=10;$i++){

echo $i;

}

}

displayIntegers();

var_dump(xdebug_get_code_coverage());

/* displays the following

12345678910


array

‘pathtoexampleflow_debugging_example.php’ =>

array

4 => int 1

5 => int 1

6 => int 1

7 => int 1

8 => int 1

9 => int 1

10 => int 1

*/


As shown in the above example, the pair of “xdebug_start_code_coverage()” and “xdebug_get_code_coverage()” functions permits us to determine with relative ease the program flow of a PHP script. In this case, the tracking process is started with the “xdebug_start_code_coverage()” function, and finalized with its counterpart, “xdebug_get_code_coverage().”

Undoubtedly, using the previous functions for debugging the execution flow of a specified PHP program should be pretty simple to understand for you, since this is pretty much a no-brainer process. 

Okay, at this stage, you hopefully grasped the logic that stands behind utilizing the  “xdebug_start_code_coverage()” and “xdebug_get_code_coverage()” functions to determine the execution sequence of a PHP program.

However, it would be interesting to see how these functions can be used when a script calls different methods of a class. Therefore, in the last section of this tutorial I’ll be coding an example that will represent this particular situation.

To see how this last practical example will be developed, please click on the link that appears below and keep reading.

{mospagebreak title=Using the xdebug_start_code_coverage() and xdebug_get_code_coverage() functions with a basic class}

In the previous section, I showed you how to use of the “xdebug_start_code_coverage()” and “xdebug_get_code_coverage()” functions to keep track of which lines are executed by a simple PHP script. Now it’s time to demonstrate how these handy functions can be employed within a PHP application that works with a basic class. To do this, I’m going to use the sample “User” class that was listed in the first section of this article, which naturally should be very familiar to you.

That being explained, here’s the complete signature that corresponds to this sample 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->lastName;

}

// get email

public function getEmail(){

return $this->email;

}

}


Having listed the entire signature of the above “User” class, it’s time to see how the “xdebug_start_code_coverage()” and “xdebug_get_code_coverage()” functions can be used conjunctly to display the lines that are executed when some methods of this class are called in a predefined sequence. Here’s the script that performs this process:


try{

xdebug_start_code_coverage();

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

echo ‘First Name :’.$user->getFirstName().'<br />';

echo ‘Last Name :’.$user->getLastName().'<br />';

echo ‘Email :’.$user->getEmail().'<br />';

var_dump(xdebug_get_code_coverage());

 

/* displays the following

First Name :John

Last Name :Doe

Email :john@domain.com

array

‘pathtoexampleflow_debugging_example.php’ =>

array

8 => int 1

11 => int 1

14 => int 1

17 => int 1

18 => int 1

19 => int 1

20 => int 1

23 => int 1

27 => int 1

31 => int 1

36 => int 1

37 => int 1

38 => int 1

39 => int 1

41 => int 1

}

catch(Exception $e){

echo $e->getMessage();

exit();

}


Despite the triviality of the above example, it serves to demonstrate how the “xdebug_start_code_coverage()” and “xdebug_get_code_coverage()” functions can be used for keeping track of which program lines are executed when calling the methods of the previous “User” class.

Logically, it’s possible (and recommendable, actually) to test these functions with more complex applications, but my intention here is simply to provide you with the right pointers for employing them for debugging the flow of your own PHP programs with more detail.

Final thoughts

In this fourth installment of the series, I discussed the usage of the complementary “xdebug_start_code_coverage()” and “xdebug_get_code_coverage()” functions to debug the execution flow of a PHP program. These functions allow you to very simply keep track of which lines are executed by an application.

This educational journey has not ended yet. Thus, in the next article I will explain how to use this pair of X-debug functions for working with conditional statements.

Now that you know what the next part will be about, you won’t want to miss it!

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

chat