Home arrow PHP arrow Page 4 - Debugging Program Flow with the Xdebug Extension

Using the xdebug_start_code_coverage() and xdebug_get_code_coverage() functions with a basic class - PHP

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.

TABLE OF CONTENTS:
  1. Debugging Program Flow with the Xdebug Extension
  2. Review: getting information about PHP objects with the var_dump() function
  3. Debugging program flow with the xdebug_start_code_coverage() and xdebug_get_code_coverage() functions
  4. Using the xdebug_start_code_coverage() and xdebug_get_code_coverage() functions with a basic class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 4
February 23, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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!



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: