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

Introducing persistent objects in PHP 5 - 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 order to explain how to create persistent objects in PHP 5, I’m going to begin building a simple class. It will use PHP’s native session mechanism to save a particular state of an object that stores some trivial data on a fictional user.

You'll understand the way this class will work much better if you see its initial definition, so below I included its partial source code. Pay close attention 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];

}

}

}

As I explained a few moments ago, all that the above “User” class does is store an instance of itself in a simple session variable, in this particular case referenced as $_SESSION[‘user’]. This specific instance will first be created via a static factory method, which will load this object either from the session variable, or as an empty entity via a regular “new” construct.

Also, for the sake of making the class’s code a bit more compact I decided to implement the “__set()” and “__get()” magic PHP methods to assign and retrieve properties dynamically through the $data internal array. Naturally, the same process can be performed by declaring those properties within the class’s body.

Well, now that I have outlined how the previous “User” class functions, hopefully you’ll have a better idea of how to create objects that can save their state to a session variable, thus persisting across several HTTP requests.

But, wait a minute! The class in question actually doesn’t implement a method that saves an instance of itself. Where is that method, then? To be honest, it needs to be defined first, and then properly implemented.

However, this process will be discussed in detail in the section to come. So, to learn more about it, click on the link shown below and read the following segment.



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