If you didn’t have the chance to learn how to use the “xdebug_start_code_coverage()” and “xdebug_get_code_coverage()” functions for keeping track of the sequence of lines executed by a PHP application, then you’re in luck; here’s an example created in the preceding article of this series, which demonstrates how to utilize these functions along with a simple PHP class. Take a look at the corresponding code sample, please: 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; } } xdebug_start_code_coverage(); $user=new User('John','Doe','john@domain.com'); if(rand(1,10)<5){ echo 'First Name :'.$user->getFirstName().'<br />'; echo 'Last Name :'.$user->getLastName().'<br />'; } else{ echo 'Email :'.$user->getEmail().'<br />'; } var_dump(xdebug_get_code_coverage()); /* displays the following when 'rand()' function returns a value < 5 First Name :John Last Name :Doe array '/path/to/example/program_flow_debug.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 36 => int 1 37 => int 1 38 => int 1 39 => int 1 40 => int 1 44 => int 1 // displays the following when the 'rand()' function returns a value > 5 Email :john@domain.com array '/path/to/example/program_flow_debug.php' => array 8 => int 1 11 => int 1 14 => int 1 17 => int 1 18 => int 1 19 => int 1 20 => int 1 31 => int 1 36 => int 1 37 => int 1 42 => int 1 44 => int 1 */ As illustrated by the above hands-on example, the “xdebug_start_code_coverage()” and “xdebug_get_code_coverage()” functions can be very useful when it comes to finding out the sequence of lines executed by a PHP script. In this specific case, this pair of complementary functions is utilized in conjunction with a basic class and a simple conditional “if” statement, with the purpose of showing how the two blocks of code that compose the prior script are called by the PHP engine. Not too difficult to grasp, right? Well, at this point I should assume that you’re pretty familiar with using the “xdebug_start_code_coverage()” and “xdebug_get_code_coverage()” functions to control the flow of a specified PHP program. Therefore, it’s time to continue exploring other functions of the library. As I stated in the beginning, this extension also offers a function called “xdebug_time_index().” As its name suggests, it can be used for timing PHP scripts with minor hassles. Therefore, in the section to come I’ll be explaining how to use this function for benchmarking purposes. This topic will be discussed in the next few lines, so click on the link that appears below and keep reading.
blog comments powered by Disqus |
|
|
|
|
|
|
|