Using the Xdebug Extension`s xdebug_call_function() Function

Among the numerous PHP debugging libraries available these days on the web, there’s one that offers easy installation and a flat learning curve, along with powerful features that permit you to debug PHP applications with minor hassles. I’m talking about the Xdebug extension, of course, and in this second part of a seven-part series you’ll learn how to use its xdebug_call_function () function.

Introduction

The Xdebug extension is a software package that will let you keep track of various useful debugging parameters. These include the state of the variables handled by your PHP programs, stack and functions calls, execution time of scripts and memory usage, and many more.

If you had the chance to read the first chapter of this series, then you have an idea of how to put the Xdebug extension to work for you, at least at a basic level. In that article, I explained how to install the library on a Windows-based web server, and developed a couple of hands-on examples that demonstrated how to use its "xdebug_call_file()" and "xdebug_call_line()" functions.

As you’ll surely recall, the first of these functions was used to return the name of the file that called a method defined within a sample class, while the second one determined at which line of the script this call was made. Indeed, the combination of these functions permits you to perform basic debugging operations in PHP programs with remarkable simplicity.

The Xdebug extension comes equipped with many other functions, however, that allow you to debug PHP applications more deeply. Therefore, in this second article of the series, I’ll be discussing how to use another useful function included with the extension, called "xdebug_call_function()," which as its name suggests, can be utilized for keeping track of the functions called by a PHP script.

Are you ready to learn how to use this handy function? Then click on the link that appears below and start reading!

{mospagebreak title=Review: the xdebug_call_file() and xdebug_call_line() functions}

As I stated in the beginning, the Xdebug extension includes a couple of useful methods, called "xdebug_call_file()" and "xdebug_call_line()" respectively, which can be utilized conjunctly for determining from which file a function or statement has been called, as well as the line number at which this call was made.

However, it would be helpful to recall how these functions can be used in two concrete cases. Therefore, below I reintroduced a couple of examples created in the previous article, which demonstrate how to utilize the methods within a sample class. Here they are:

(example on using the ‘xdebug_call_file()’ function)


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;

}

// get file from which this method was called

public function getFile(){

return ‘Called from file ‘.xdebug_call_file();

}

}


try{

$user=new User(‘John’,’Doe’,’john@domain.com’);

echo $user->getFile();

 

/* displays the following

Called from file C:pathtofiledebug_call_file_example.php

*/

 

}

catch(Exception $e){

echo $e->getMessage();

exit();

}



(example on using the ‘xdebug_call_line()’ function)


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->firstName;

}

// get email

public function getEmail(){

return $this->email;

}

// get line from which this method was called

public function getLine(){

return ‘Called from line ‘.xdebug_call_line();

}

}


try{

$user=new User(‘Alejandro’,’Gervasio’,’alejandro@domain.com’);

echo $user->getLine();

 

/* displays the following

Called from line 40

*/

}

catch(Exception $e){

echo $e->getMessage();

exit();

}


Despite the simplicity of the examples listed above, they do demonstrate how to utilize the "xdebug_call_file()" and "xdebug_call_line()" functions to find out from which file a class method has been invoked, and from which program line this call was performed.

In both cases, for example purposes, these functions were called from inside a sample class, but it’s valid to use them when working with procedural functions as well.

Well, at this stage you hopefully are well prepared for using these two functions in your own PHP applications, if you want to debug them at a basic stage. Therefore, it’s time to explore a few other handy functions that come bundled with the Xdebug extension.

In the following section I will explain how to use the "xdebug_call_function()" function to find out from which function a method or statement has been called when a PHP script is being executed.

To learn how this brand new function will be utilized in a useful way, please click on the link shown below and keep reading.

{mospagebreak title=Using the xdebug_call_function() function}

Quite often when debugging a PHP application, it’s necessary to find out from which function a method or statement has been called. To accomplish this task with minor efforts, the Xdebug extension provides the "xdebug_call_function()" function (the name isn’t redundant, trust me).

As usual, to demonstrate how this function can be used in a concrete situation, I’m going to appeal to the functionality of the "User" sample class that was built in the first article. However, in this case, this class will implement a new method, called "getFunction()," which will show you how to work with the "xdebug_call_function()" function.

Here’s the modified signature of this class, so pay attention to the way it functions:


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;

}

// get the function that was called

public function getFunction(){

return ‘Called from function ‘.xdebug_call_function();

}

}


As you can see, the above "User" class now declares and implements a new method called "getFunction()" that internally uses the "xdebug_call_function()" to return to client code the name of the function that invoked the method.

As with the other functions discussed in the preceding article, this one can also be used when debugging procedural PHP programs, so it’s clear to see that there’s plenty of room for experimenting with this brand new function.

Now, and returning to the implementation of the above "getFunction()" method, it should be extremely easy to grasp for you, even though it still hasn’t been used in the context of a practical example. Don’t worry; in the following section I will write a short code sample that will help to demonstrate the actual functionality of the "xdebug_call_function()."

To learn how this new example will be developed, please click on the link below and read the next few lines.

{mospagebreak title=Testing the xdebug_call_function()}

To demonstrate how useful the "xdebug_call_function()" can be for debugging purposes, I’m going to build a simple script which will make use of the "User" class that you saw in the previous section. In this case, the script simply will be tasked with creating a new instance of this class, and then with calling its "getFunction()" method.

The result of this operation can be seen in the code sample below:


try{

$user=new User(‘John’,’Doe’,’john@domain.com’);

echo $user->getFunction();

 

/* displays the following

Called from function {main}

*/

}

catch(Exception $e){

echo $e->getMessage();

exit();

}


Well, if you were expecting an output of one hundred lines, then probably you’ll be a bit disappointed with the result produced by the above example. However, it’s pretty illustrative, since it shows in a nutshell how the "xdebug_call_function()" really works. In this particular example, the function has been wrapped within a class method, so it returns the name of the function from which it was actually called, that is the C "main" function.

With this basic example, it’s clear to see how deeply the xdebug_call_function()" can dig when it comes to determining from which function a method or statement has been invoked. Simple and effective, right?

And finally, as usual with many of my articles on PHP development, feel free to tweak all of the code samples shown in this tutorial to give you some practice in using the "xdebug_call_function()" function provided by the Xdebug extension. You’ll have a good time doing this, guaranteed!

Final thoughts

In this second installment of the series, I walked you through using the "xdebug_call_function()" function, bundled with the X-debug extension, to keep track of the different functions calls triggered by a PHP script.

In the next part, I will explain how to use the enhanced version of the "var_dump()" PHP built-in function to retrieve detailed information about a specified variable. Now that you know the subject of the upcoming article, you won’t want to miss it!

[gp-comments width="770" linklove="off" ]

chat sex hikayeleri Ensest hikaye