Home arrow PHP arrow Page 4 - Working with the X-debug extension`s var_dump() function

Retrieving information about a PHP object with the var_dump() function - PHP

If you’re a PHP developer who’s searching for an approachable guide to using the most relevant functions that come bundled with the X-debug extension, then look no further. Welcome to the third article of a series on debugging in PHP with the X-debug extension. Comprised of seven tutorials, this series teaches you how to utilize the features that come with the X-debug library to debug your own PHP applications.

TABLE OF CONTENTS:
  1. Working with the X-debug extension`s var_dump() function
  2. Review: the xdebug_call_function() method
  3. Using the enhanced version of the var_dump() PHP function
  4. Retrieving information about a PHP object with the var_dump() function
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
February 17, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Since the "var_dump()" function accepts any type of PHP variable as an incoming parameter, it permits you to retrieve meaningful information about objects as well. Of course, this feature can be quite useful when developing and debugging applications that use the object-oriented paradigm, so if you create PHP programs that rely on classes for doing their business, then you'll find the "var_dump()" function fairly appealing.

However, it's necessary to develop an example that demonstrates how this function actually works when utilized on a specified object. So, to do that I'm going to use the same sample "User" class that you saw in the beginning, whose signature looked like this:


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;

}

}


Now that there's a basic class available for testing purposes, it's time to pass to the "var_dump()" function an instance of it, so you can see clearly how it retrieves useful information about a simple object. The following code sample feeds the function a trivial array that also includes an object spawned from the above "User" class. Take a look at it:


try{

$user=new User('John','Doe','john@domain.com');

$data=array('string'=>'This is a string','integer'=>1,'float'=>0.123456,'object'=>$user);

var_dump($data);

 

/* displays the following

array

'string' => string 'This is a string' (length=16)

'integer' => int 1

'float' => float 0.123456

'object' =>

object(User)[1]

private 'firstName' => string 'John' (length=4)

private 'lastName' => string 'Doe' (length=3)

private 'email' => string 'john@domain.com' (length=15)

*/

}

catch(Exception $e){

echo $e->getMessage();

exit();

}


Not too bad, right? As you can see in the above example, the "var_dump()" function returns detailed information to client code about the $user object passed in as an incoming argument. It displays the name of the originating class, along with the values and lengths (expressed in characters) assigned to the object's properties.

With this particular example, it's clear to see how the X-debug library extends the functionality of the "var_dump()" PHP built-in function. If you debug your programs' variables by means of this function, then its enhanced version will put a big smile in your face.

Final thoughts

Over this third part of the series, I provided you with a quick overview of the improved version of the PHP native "var_dump()" function included with the X-debug extension. As you saw, it can be really useful for debugging program variables with a greater level of detail.

In the forthcoming installment, I'll be exploring the set of functions bundled with X-debug that permit you to keep track of each line executed by a PHP script. Do you want to learn how these functions work? Then don't miss the next article!



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

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

 


Dev Shed Tutorial Topics: