Home arrow PHP arrow Page 4 - The X-debug Extension

Working with the xdebug_call_line() function - PHP

If you program at all, you understand the necessity of debugging. Fortunately, depending on the language in which you program, you can find an assortment of tools to help you with this task. One very good debugging tool for PHP is the X-debug extension. This seven-part series will reveal its capabilities and help you add it to your programmer's toolbox.

TABLE OF CONTENTS:
  1. The X-debug Extension
  2. Getting started using the X-debug library
  3. Keeping track of file calls with the X-debug extension
  4. Working with the xdebug_call_line() function
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
February 02, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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!



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 



© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap

Dev Shed Tutorial Topics: