Home arrow PHP arrow Page 3 - Keeping Track of Objects when Using Destructors in PHP 5

Retrieving useful information about a particular object before it's removed - PHP

Among the improvements that were introduced into the object model of PHP 5, class destructors are quite possibly, one of the easiest to learn and implement. As you may have heard, a destructor is a special kind of method that is called automatically by the PHP interpreter right before destroying a particular object. So if you’re interested in learning how to use them in your own PHP 5-driven applications, you should start reading this article series!

TABLE OF CONTENTS:
  1. Keeping Track of Objects when Using Destructors in PHP 5
  2. When the order really matters: working with multiple destructors
  3. Retrieving useful information about a particular object before it's removed
  4. Seeing the improved User class in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
January 30, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

In accordance with the concepts that I deployed in the section you just read, I'm going to modify the destructor of the sample "User" class that you learned previously so that it can display some useful information, such as methods and properties, about an object that is about to be destroyed.

To achieve this process as painlessly as possible, below I included the improved definition of the "User" class, which this time implements its destructor in a more useful way. Have a close look at its definition, please:


// define 'User' class

class User{

private $firstName;

private $lastName;

private $email;

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

if(!$firstName||strlen($firstName)>32){

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

}

if(!$lastName||strlen($lastName)>32){

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

}

if(!$email||!preg_match("/^.+@.+..+$/",$email)){

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

}

$this->firstName=$firstName;

$this->lastName=$lastName;

$this->email=$email;

}

// 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>';

$methods=get_class_methods(get_class($this));

foreach($methods as $method) {

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

}

}

}


As you can see, things are getting really exciting now, since the above user handling class offers a more insightful implementation for its destructor. In this specific case, the destructor in question uses some well-known PHP native functions, like "get_object_vars()" and "get_class_methods()," in order to display some useful information about a particular object prior to its deletion.

Of course, there is plenty of room to experiment and have fun here by implementing the destructor in different ways, according to your specific needs. But this hands-on example should give you an approximate idea of how to define a destructor that eventually provides relevant data on a specific object.

So far, so good. At this point I've already taught you how to implement a destructor and provide it with the ability to display the values of the methods and properties assigned to a specific object, right before its destruction.

However, if you're as curious as me, then surely you'll want to see how this method works in the context of a practical example. Therefore, in order to learn how this example will be constructed, please visit the following section. It's only one click away.



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