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