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:
/* displays the following
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.
blog comments powered by Disqus