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

Working with multiple objects by redefining the destructor of the previous “User” 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.

  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



Since my purpose here is to demonstrate how multiple destructors are called by the PHP parser, when working with several objects I’m going to modify the signature of the previous “User” class and more specifically, its destructor method.

Basically, the method in question will now be capable of indicating what object is being used before the PHP interpreter removes it from memory (destroyed, in crude terms). Now that you know the scenario, please take a look at the modified definition of the sample user class, which is as follows:

// define 'User' class (it provides a slightly more complete implementation of its destructor)

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

echo '<h2>Warning! Destroying User: '.$this->firstName.' '.$this->lastName.'<h2>';



Now, the destructor method of the above “User” class has been provided with the capacity for displaying on the browser the first and last name properties of the object that is about to be destroyed. This is convenient for finding out the order in which destructors are called when using multiple objects.

At this stage, having improved the implementation of the destructor that belongs to the previous user class, it’s appropriate for you to read the next section of the article. In it you’ll learn how to keep track of the order used by the PHP parser to call several destructors when using different user objects.

This juicy topic will be discussed in detail in the next few lines, so go ahead and read them please.

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