Home arrow PHP arrow Page 2 - Implementing Destructors with Multiple Objects in PHP 5

Handling user-related data with a simple PHP 5 class - PHP

Welcome to the second installment of the series “Understanding Destructors in PHP 5.” Whether you’re a PHP programmer who’s making their first steps in the area of professional PHP development, or an experienced developer with a solid background in object-based PHP programming, this article series can provide you with an approachable guide for using destructors in PHP 5-controlled development environments.

TABLE OF CONTENTS:
  1. Implementing Destructors with Multiple Objects in PHP 5
  2. Handling user-related data with a simple PHP 5 class
  3. Working with multiple objects by redefining the destructor of the previous “User” class
  4. Finding out the order in which several destructors are called by the PHP interpreter
By: Alejandro Gervasio
Rating: starstarstarstarstar / 6
January 23, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Before I teach you how to work with multiple classes that provide concrete implementation for each of their destructors, it’s convenient to refresh a few fundamental concepts concerning the use of them at a very basic level. To do so, I’m going to use the sample class that I constructed in the last tutorial. As you'll recall, it came in handy for managing common user-related data, including first and last names and email addresses.

Essentially, the signature of this user handling class looked like this:

// basic example on using a '__destruct()' method

// 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(){

echo '<h2>Destroying user now...<h2>';

}

}


Frankly speaking, I must say that the definition of the above “User” class is extremely easy to understand. So you shouldn’t have much of a problem grasping how it functions. However, I’d like you to pay close attention to the “__destruct()” method declared in the end. In this case, as with all of the magic methods provided by PHP 5, it’s preceded by two underscore (__) characters and indicates that the interpreter is going to execute it right before destroying an object created with this class.

This concept may sound rather confusing if it’s not accompanied by a practical example. Therefore, below I coded a simple script that spawns a new user object, displays its properties' values on the browser, and then, as you’d expect, triggers the corresponding destructor prior to finishing its execution.

Having explained that, please take a look at the following code sample:

try{

// create new instance of 'User' class

$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

Destroying user now...

*/

}

catch(Exception $e){

echo $e->getMessage();

exit();

}


There you have it. Check the above example with your own testing web server and you’ll be amazed at how well it works! Perhaps it’s not going to change your life as a PHP programmer forever, but undoubtedly it’s going to show you how a destructor functions. In this case, the implementation of the destructor is rather primitive, yet this example should fire up your creativity so that you use your own class destructors in a more useful way.

Well, at this moment you’ve hopefully recalled the foundations of working with destructors in PHP 5. So assuming that this is true, the next thing that I’m going to teach you will be how to use multiple objects that concretely implement their respective destructors. In doing this, you’ll be able to see very clearly in what order these methods are called by the PHP interpreter right before the objects are destroyed.

To find out how this will be done, 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
   

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: