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!
blog comments powered by Disqus |
|
|
|
|
|
|
|