Home arrow PHP arrow Page 3 - Understanding Destructors in PHP 5

Introducing Class Destructors - PHP

Despite the scary-sounding name, destructors aren't evil. Indeed, their use is often necessary to help keep your applications running properly. This article, the first part of a five-part series, introduces you to destructors and their use with PHP 5 programs.

  1. Understanding Destructors in PHP 5
  2. Building a Sample
  3. Introducing Class Destructors
  4. Testing the Previous User
By: Alejandro Gervasio
Rating: starstarstarstarstar / 13
January 16, 2008

print this article



Calling a concrete method before destroying an object: introducing class destructors

In the previous section, you learned how to build a simple class that didn’t define a destructor method inside its API, which could be necessary under certain conditions. Of course, I’m not saying that from this point onward each class you work with must declare a destructor, because this would be pointless.

However, let me show you a modified version of the “User” class that you learned before. This time I will include a very primitive “__destruct()” method. Please study the brand new definition of the class in question:

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

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

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



As you can realize, defining a destructor for a determined PHP 5 class isn’t rocket science; the whole process is reduced to defining a “__destruct()” method, and then implementing it correctly. In this case, my destructor is actually very primitive and will display the message “Destroying user now…” before the corresponding object is destroyed.

So far, so good. At this point, you've grasped the basics of defining a destructor within the previous “User” class, something that hopefully has caught your attention. Therefore, I'm going to create a test example, aimed at demonstrating the limited functionality of the destructor we just created.

To see how this brand new example will be developed, please visit the next 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: