As I mentioned in the previous section, the X-debug extension includes another useful function, called “xdebug_call_line(),” which returns the line number from which a method, function or statement has been invoked. To demonstrate how this function works, I’m going to modify the signature of the sample “User” class that you learned before, which now will use the “xdebug_call_line()” function for implementing a brand new method, named “getLine().” That being said, here’s the modified signature of the “Users” class: 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(); } } Here, it’s clear to see how the above “getLine()” method defined within the “User” class uses the “xdebug_call_line()” function to return to client code the line from which the method in question has been called. Does this sound rather confusing? It’s not, really. The sample script shown below should give you a clear idea of how the “xdebug_call_line()” function works. Here it is: try{ $user=new User('John','Doe','john@domain.com'); echo $user->getLine();
/* displays the following Called from line 40 */ } catch(Exception $e){ echo $e->getMessage(); exit(); } See how simple it is to utilize the “xdebug_call_line()” function within a sample class? I hope you do! In this specific case, the function returns the line from which the “getLine()” method has been called. Simple and useful too. Of course, this function may seem a bit primitive to be used for debugging purposes. But, when combined with the “xdebug_file_call()” function that you saw before, you can get more detailed information on the way that your script works. Try them out for yourself and see how they behave in each case. Final thoughts In this first chapter of the series, I provided you with a quick introduction to installing and using the X-debug PHP extension for debugging a couple of simple PHP scripts. As I said in the introduction, this extension is pretty simple to configure and use, and these features make it suitable for both beginners and advanced programmers. In the upcoming article, I’ll be explaining how to use X-debug for keeping track of the functions being called during the execution of a PHP script. Now that you know the topic for the next tutorial, don’t miss it!
blog comments powered by Disqus |
|
|
|
|
|
|
|