One of the most basic features that any decent PHP debugging library must have is the ability to keep track of which files are used by a script or application, including files that are loaded via “include” directives. Fortunately, the X-debug extension provides this information in an easy-to-read format via its “xdebug_call_file().” As its name suggests, it returns the file name of the file being called for a PHP script. Although the functionality of this function is pretty simple to grasp, the best way to understand how it works is by setting up a concrete example. With this idea in mind, I’m going to define a simple class, which will implement one of its methods with the aforementioned “xdebug_call_file()” function. Here is how this sample class looks: 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(); } } Certainly, I’m not going to waste your time explaining how the above “User” class works, since this would be pretty pointless. You should, however, pay attention to the implementation of its “getFile()” method. This method internally uses the “xdebug_call_file()” function of the X-debug extension to return to client code the name of the file being called by the current script. This shouldn’t be difficult to grasp, but to dissipate any possible doubts, please take a look at the following code sample. It shows the output generated by the “xdebug_call_file()” function. Here it is: 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(); } As shown above, the “xdebug_call_file()” function returns the name of the file from which the “getFile()” method was called, which makes it really easy to keep track of the different files that are used by a PHP script. In this case, I decided to keep the previous example as simple as possible, to help you understand more easily how this function works in a concrete situation. At this point, I’m sure that you grasped how to utilize the prior “xdebug_call_file()” function to determine from which file a function, method or statement has been called when running a PHP script. Nonetheless, the X-debug extension offers another function, called “xdebug_call_line(),” which can be used for determining from which line an instruction has been invoked. In the upcoming segment, I’m going to illustrate how to use this brand new debugging function. Please click the link that appears below and read the next few lines.
blog comments powered by Disqus |
|
|
|
|
|
|
|