Home arrow PHP arrow Page 3 - The Destruct Magic Function in PHP 5

Working with destructors in PHP 5 - PHP

Welcome to the sixth part of a seven-part series that shows you how to use the magic functions that come with PHP 5. In this article, I cover the “__destruct()” method, also known as a destructor. They can be really useful for performing all sorts of clean-up tasks, or for creating objects that are capable of maintaining their state across several HTTP requests.

TABLE OF CONTENTS:
  1. The Destruct Magic Function in PHP 5
  2. Review: the clone magic function
  3. Working with destructors in PHP 5
  4. Creating a persistable object with the destruct method
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
June 23, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
 

As I stated in the previous segment, it’s possible to accomplish interesting and useful things with a destructor. In this case, I’m going to use it within the already familiar “User” class to create an object that will save itself to a session variable before being destroyed. 

How will this be achieved? Well, take a look at the enhanced definition of the “User” class, which now implements the “__destruct()” method. Here it is:

 

// define 'User' class (implements the __destruct() magic method)

class User

{

// constructor (not implemented)

public function _construct(){}

 

// set undeclared property in a restrictive way

public function __set($property, $value)

{

if (in_array($property, array('fname', 'lname', 'email')) === TRUE)

{

$this->$property = $value;

}

}

 

// get declared property

public function __get($property)

{

if (isset($this->$property))

{

return $this->$property;

}

}

 

// single point to fetch user data

public function __call($method, $args)

{

if ($method === 'fetch' AND empty($args) === FALSE)

{

return $this->$args[0];

}

}

public function __destruct()

{

session_start();

$_SESSION['user'] = serialize($this);

}

}

 

As shown above, the implementation of the “__destruct()” method is very easy to follow. All it does is start or resume a new session, and then save an instance of the “User” class to a session variable. Period. 

Logically, nothing spectacular is happening here, except for the fact that all of these tasks will be performed before the instance of the user class gets destroyed. In this manner we're building an object that has the ability to maintain its state across different HTTP requests, by using only the PHP built-in session mechanism. 

Even though the definition of the “__destruct()” method is indeed self-explanatory, an example is in order her to understand how it works. Therefore, in the last section of this tutorial I’m going to set up such an example, so you can see how the destructor does its thing. 

To learn how this wrapping example will be created, click on the link shown below and keep reading. We’re almost done exploring destructors!

 



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