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