Tracking a Stack of Function Calls with the Xdebug Extension

If you’re a PHP programmer looking for a comprehensive guide to using the most relevant functions that come with the X-debug extension, then this set of articles might be what you need. Welcome to the last part of a series focusing on debugging in PHP with the Xdebug extension. In seven approachable parts, this series gets you started utilizing this library’s numerous features by way of a hands-on approach.

Introduction

Now that you’ve been introduced to the main subject of this series, it’s time to recapitulate the topics that were treated in the last installment, so you can see more clearly how to link them with the ones that I plan to discuss in this final part.

So, in simple terms, in the aforementioned article I explained how to take advantage of the functionality provided by the “xdebug_time_index()” function to build a couple of timing scripts. I started by using a procedural approach, and then took advantage of the object-oriented paradigm.

In the first case, this function in question was utilized simply for measuring the execution time of a primitive “for” loop, which was very simple to grasp. In the second example, things were more complicated, since it was used as the workhorse of a timer class. Anyway, both examples should be considered only as an introduction to using this handy function in more complex situations.

In addition, the Xdebug extension comes bundled with a few other functions, which, despite their potential functionality, simply can’t be covered completely in a series like this. Despite this inconvenience, in this final part of the series, I’m going to show you how to work with a function of the library called “xdebug_get_function_stack().” This function can be used to keep track of the stack of function calls generated by a PHP script, in this manner completing this starting guide on this helpful extension.

Does this sound good enough for you? Then start reading!

{mospagebreak title=Review: the xdebug_time_index() function}

First, I’d like to quickly review the practical example developed in the preceding article of this series, in case you haven’t had a chance to read that article. The example was aimed at demonstrating how to use the “xdebug_time_index()” function for building a pair of timing scripts.

That being said, here are the two code samples that create those timing scripts. Have a look at them, please:

(example on using the ‘xdebug_time_index()’ function within a procedural script)


echo ‘Starting time : ‘.xdebug_time_index().’<br />’;

for($i=0;$i<=3;$i++){

// do nothing

sleep(1);

}

echo ‘Total script execution time : ‘.xdebug_time_index();


/* displays the following


Starting time : 0.001168966293335

Total script execution time : 3.9993019104004

*/



(example on using the ‘xdebug_time_index()’ function within an object-based script)


class Timer{

public function __construct(){}

public function getTime(){

return xdebug_time_index();

}

}


$timer=new Timer();

echo ‘Starting time :’.$timer->getTime().’<br />’;

for($i=0;$i<=5;$i++){

sleep(1);

}

echo ‘Total script execution time :’.$timer->getTime();

/* displays the following


Starting time :0.0012168884277344

Total script execution time :5.9927868843079


*/


As you can see, the examples listed above illustrate how to use the “xdebug_time_index()” function to build a few primitive timing applications. In the first case, the function is utilized in the context of a procedural script, while the second example is a bit more interesting, since it shows how to create a basic timer class by using the function. Quite simple to grasp, right?

Hopefully, at this point you already realized how useful the “xdebug_time_index()” function can be when it comes to calculating the execution time of a certain PHP application. So what’s next? Well, as I said in the introduction, the Xdebug extension includes yet another handy function, named “xdebug_get_function_stack(),” that permits us to explore with relative ease the stack of function calls generated when a script is parsed by the PHP engine.

Therefore, in the next section I’ll be explaining how to use this function in a concrete situation. In fact, I’ll use a hands-on example to demonstrate it explicitly. This way, you can get a better idea of its actual functionality.

To learn how this brand new example will be developed, go ahead and read the next segment. It’s only one click away.

{mospagebreak title=Retrieving information with the xdebug_get_function_stack() function}

To show you the use of the “xdebug_get_function_stack()” function as clearly as possible, I’m going to build another sample class. It will be tasked with performing a pretty trivial task – decorating any string passed in as an incoming argument with a few stars.

Regardless of the simplicity of the logic implemented by this class, it should illustrate how to use the “xdebug_get_function_stack()” function in a specific case.

Here’s the signature corresponding to this sample class, which I dubbed “NameDecorator”:


class NameDecorator{

public function displayStars(){

var_dump(xdebug_get_function_stack());

return str_repeat(‘*’,10);

}

public function displayDecoratedName($names=array()){

foreach($names as $name){

echo $this->displayStars().’ ‘.$name.’<br />’;

}

}

} 


Definitely, the signature of the above “NameDecorator” class is pretty easy to follow, isn’t it? As shown before, this class declares and implements only two basic methods. The first one is charged with generating a string composed of ten star characters, which will be added to each name passed in as an input parameter.

This “decorative” process will be performed by the second “displayDecoratedName()” method, which you should grasp in a snap. However, the most important thing to note here is that the class will make use of the “xdebug_get_function_stack()” function to display the content of the stack of function calls.

Now that there’s a sample class available for illustrating how the “xdebug_get_function_stack()” function does its thing, it’s time to create a final example to show you how this function can keep track of the stack of methods called by the “NameDecorator” class.

Wait are you waiting for? Click on the link  below and read the following section.

{mospagebreak title=Displaying the contents of the stack of function calls}

In the previous section, I built a simple decorative class that added a few star characters to an inputted string. The most relevant aspect of this class, however, is obviously that it employs internally the “xdebug_get_function_stack()” function, meaning that it’s necessary to code a short example that demonstrates its functionality.

Therefore, below I included a simple code sample. It shows the output generated by this function when called from inside the “NameDecorator” class:

$nameDec=new NameDecorator();

$nameDec->displayDecoratedName(array(‘John’,'Mary’,'Susan’));


/* displays the following

array

0 =>

array

‘function’ => string ‘{main}’ (length=6)

‘file’ => string ‘/path/to/example/stack_call_example.php’ (length=72)

‘line’ => int 0

‘params’ =>

array

empty

1 =>

array

‘function’ => string ‘displayDecoratedName’ (length=20)

‘class’ => string ‘NameDecorator’ (length=13)

‘file’ => string ‘/path/to/example/stack_call_example.php’ (length=72)

‘line’ => int 15

‘params’ =>

array

empty

2 =>

array

‘function’ => string ‘displayStars’ (length=12)

‘class’ => string ‘NameDecorator’ (length=13)

‘file’ => string ‘/path/to/example/stack_call_example.php’ (length=72)

‘line’ => int 10

‘params’ =>

array

empty


********** John


array

0 =>

array

‘function’ => string ‘{main}’ (length=6)

‘file’ => string ‘/path/to/example/stack_call_example.php’ (length=72)

‘line’ => int 0

‘params’ =>

array

empty

1 =>

array

‘function’ => string ‘displayDecoratedName’ (length=20)

‘class’ => string ‘NameDecorator’ (length=13)

‘file’ => string ‘/path/to/example/stack_call_example.php’ (length=72)

‘line’ => int 15

‘params’ =>

array

empty

2 =>

array

‘function’ => string ‘displayStars’ (length=12)

‘class’ => string ‘NameDecorator’ (length=13)

‘file’ => string ‘/path/to/example/stack_call_example.php’ (length=72)

‘line’ => int 10

‘params’ =>

array

empty


********** Mary


array

0 =>

array

‘function’ => string ‘{main}’ (length=6)

‘file’ => string ‘/path/to/example/stack_call_example.php’ (length=72)

‘line’ => int 0

‘params’ =>

array

empty

1 =>

array

‘function’ => string ‘displayDecoratedName’ (length=20)

‘class’ => string ‘NameDecorator’ (length=13)

‘file’ => string ‘/path/to/example/stack_call_example.php’ (length=72)

‘line’ => int 15

‘params’ =>

array

empty

2 =>

array

‘function’ => string ‘displayStars’ (length=12)

‘class’ => string ‘NameDecorator’ (length=13)

‘file’ => string ‘/path/to/example/stack_call_example.php’ (length=72)

‘line’ => int 10

‘params’ =>

array

empty


********** Susan

*/


That was pretty interesting, right? As you can see, once the above “NameDecorator” class was fed with an input array comprised of a few trivial names, it echoed to the browser not only their decorated versions, but another array containing relevant information about the script being executed — including the file name and program lines being parsed, and the stack of functions called by the class.

Considering that all of this data was generated by the ““xdebug_get_function_stack()” function, it’s fair to say that it can be really helpful for debugging PHP applications with minor efforts.

With this final example I’m finishing this introductory guide to using the Xdebug extension. Since this library includes a lot of other functions, my suggestion is that you read its official documentation for full information on each of them. Happy debugging!

Final thoughts

It’s hard to believe, but we’ve finally come to the end of this series. Overall, the experience has been both fun and instructive, since you learned how to install the Xdebug extension and how to use its most relevant functions.

As you saw through the code samples created in the articles, the library is pretty intuitive. Best of all, it provides detailed information about common aspects of a PHP program, which can be really helpful for performing debugging tasks that require a great deal of detailed information.

See you in the next PHP web development tutorial!

[gp-comments width="770" linklove="off" ]
antalya escort bayan antalya escort bayan