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