Home arrow PHP arrow Page 2 - Retrieving Information on Several Objects with Destructors in PHP 5

Keeping track of a single object - PHP

Welcome to the fourth part of the series “Understanding Destructors in PHP 5.” Made up of five comprehensive tutorials, this series brings to you the foundations of how to declare and implement destructors in PHP 5-driven classes. And it tackles this process from a practical point of view, since you’ll be able to learn these methods by using copious examples.

  1. Retrieving Information on Several Objects with Destructors in PHP 5
  2. Keeping track of a single object
  3. Working with different user handling objects
  4. Displaying data related to different objects using one single destructor
By: Alejandro Gervasio
Rating: starstarstarstarstar / 5
February 06, 2008

print this article



We'll start demonstrating how to use a destructor to keep track of the methods and properties of multiple objects by recreating the hands-on example developed in the preceding article of the series. In that part I showed you how to perform this task, but using only one instance of the sample “User” class.

So, with that idea in mind, below I included the signature of this user handling class, along with the definition of a basic script, which outputs to the browser the properties and methods of a generic user object prior to being deleted by the PHP engine.

The pertinent code sample is as follows:

// define 'User' class

class User{

private $firstName;

private $lastName;

private $email;

public function __construct($firstName,$lastName,$email){


throw new Exception('Invalid First Name parameter!');



throw new Exception('Invalid Last Name parameter!');



throw new Exception('Invalid Email parameter!');






// get user's first name

public function getFirstName(){

return $this->firstName;


// get user's last name

public function getLastName(){

return $this->lastName;


// get user's email

public function getEmail(){

return $this->email;


// get all user data

public function getAll(){

return 'First Name: '.$this->firstName.' Last Name: '.$this->lastName.' Email Address: '.$this->email;


// implement a __destruct()' method

public function __destruct(){

// display object properties

echo '<h2>Properties of object being destroyed</h2>';

foreach(get_object_vars($this) as $prop=>$val) {

 echo '<p>'.$prop.'='.$val.'</p>';


// display object methods

echo '<h2>Methods of object being destroyed</h2>';


foreach($methods as $method) {

echo '<p> Method Name: '.$method.'()</p>';





// create user object

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

// display separately user data

echo 'First Name: '.$user->getFirstName().'<br />';

echo 'Last Name: '.$user->getLastName().'<br />';

echo 'Email: '.$user->getEmail().'<br />';

// display all user information

echo 'Complete user information: '.$user->getAll();


/* displays the following


First Name: John

Last Name: Doe

Email: john@domain.com

Complete user information: First Name: John Last Name: Doe Email Address: john@domain.com


Properties of object being destroyed





Methods of object being destroyed

Method Name: __construct()

Method Name: getFirstName()

Method Name: getLastName()

Method Name: getEmail()

Method Name: getAll()

Method Name: __destruct()




catch(Exception $e){

echo $e->getMessage();



After studying in detail the above example, you’ll have to agree with me that destructors can be pretty useful for logging purposes. As you can see, this particular case shows how to use this magic method to print the values assigned to the pertinent properties of a simple user object, in addition to showing the names of its method.

This example itself is a no-brainer, but it should give you an approximate idea of how to use a destructor to develop an effective log mechanism that can be triggered before one or more objects no longer exist in the context of a given PHP 5 application.

Please focus your attention again on the above code sample, since there’s one interesting thing to stress: as you can see, it demonstrates how to display some useful information about one particular object, but you’re probably wondering if this same process can be performed with multiple objects.

Fortunately, the answer is a resounding yes! And not only is it possible to carry out this task, but it’s extremely easy to do. Thus, in the next section, I’m going to teach you how to print, on the browser, data associated with the structure of different objects, including their methods and properties, by using the functionality brought by a single destructor.

In order to learn the details of how this process will be performed, please visit the following section and keep reading.

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- 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: