Home arrow PHP arrow Page 3 - Building Persistent Objects in PHP 5

Implementing a destructor method - PHP

The HTTP protocol is stateless, but sometimes it is necessary to make web applications store or remember information. This is sometimes referred to as persistent storage, and it takes on a number of different forms. This six-part series of articles will explain the concept and show you various ways to give your web applications a memory.

TABLE OF CONTENTS:
  1. Building Persistent Objects in PHP 5
  2. Introducing persistent objects in PHP 5
  3. Implementing a destructor method
  4. Putting the sample User class into action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 8
September 15, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

In the course of the previous segment, I partially defined a sample class, which had the ability to create and assign properties dynamically. It had this ability due to a simple implementation of the so-called property overloading process, carried out by the complementary “__set()” and “__get()” PHP magic functions.

However, the most important point to note with reference to this class is that I intend to make it capable of saving an instance of itself to a session variable. So far, its factory method does load this instance, but there’s no a specific method that first stores the object on the session variable. So how can this be done?

Well, as you may know, there are many ways to save an instance of a class to a session variable. For instance, I could define a regular method called “save()” or something similar to do this, but in this particular case I’m going to use a destructor to perform this task. In doing so, the whole saving process will be handled transparently by the PHP engine.

Of course, it’s important to learn how to implement a destructor within the previous “User” class, so below I included its basic definition:

// save object to session variable

public function __destruct()

{

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

}

Were you expecting to see lengthier code? Fortunately, this is not the case. The implementation of this destructor does only one thing, namely save an instance of the “User” class to a predefined session variable. Since a destructor method is always called automatically right before the PHP interpreter finishes parsing a script, the instance will be stored via the session mechanism in a truly transparent manner.

Now that you've grasped the underlying logic that drives the previous destructor method, it’s time to see the complete definition of the “User” class, after adding this last method to it:

class User

{

private $data = array();

 

// constructor (not implemented)

public function __construct(){}

 

// factory method

public static function factory()

{

session_start();

if(isset($_SESSION['user']) === TRUE)

{

return unserialize($_SESSION['user']);

}

return new User();

}

 

// set undeclared property

public function __set($property, $value)

{

$this->data[$property] = $value;

}

 

// get undeclared property

public function __get($property)

{

if (isset($this->data[$property]) === TRUE)

{

return $this->data[$property];

}

}

 

// save object to session variable

public function __destruct()

{

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

 

}

}

Definitely, I’m not trying to pretend that creating persistent objects in PHP 5 is always that simple, but you’ll have to admit that understanding how the above “User” class persists during different HTTP requests is a straightforward process.

Even so, a concrete example is in order here, so you can see how to work with an instance of this sample class. Thus, in the last segment of this tutorial I’m going to create a script that first assigns some properties to an object spawned from the class, and then uses its destructor to save it to the $_SESSION[‘user] variable that you saw before.

Therefore, if you wish to learn how this example will be developed, click on the link displayed below and read the next section.



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