Home arrow PHP arrow Page 2 - Using the Xdebug Extension`s xdebug_call_function() Function

Review: the xdebug_call_file() and xdebug_call_line() functions - PHP

Among the numerous PHP debugging libraries available these days on the web, there’s one that offers easy installation and a flat learning curve, along with powerful features that permit you to debug PHP applications with minor hassles. I'm talking about the Xdebug extension, of course, and in this second part of a seven-part series you'll learn how to use its xdebug_call_function () function.

TABLE OF CONTENTS:
  1. Using the Xdebug Extension`s xdebug_call_function() Function
  2. Review: the xdebug_call_file() and xdebug_call_line() functions
  3. Using the xdebug_call_function() function
  4. Testing the xdebug_call_function()
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
February 09, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



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

blog comments powered by Disqus
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


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

Dev Shed Tutorial Topics: