Home arrow PHP arrow Page 2 - Registry Design Pattern: Complete Implementation

Review: building a basic session-based registry - PHP

Welcome to the conclusion of a three-part article series that shows you how to implement the Registry design pattern in PHP. In this part, we'll take all the classes that we've created so far and put them to work together.

  1. Registry Design Pattern: Complete Implementation
  2. Review: building a basic session-based registry
  3. Defining a basic autoloader class
  4. Using the registry classes in a single script
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
July 21, 2010

print this article



Before demonstrating how to use all the sample registry classes together, I'm going to reintroduce the example created in the previous tutorial. It was aimed at showing how to build a basic session-based registry by using a few straightforward methods.

In case you don't recall it, the registry was derived from an abstract parent whose definition looked like this:



abstract class AbstractRegistry


protected static $_instances = array();

// get Singleton instance of the registry (uses the 'get_called_class()' function available in PHP 5.3)

public static function getInstance()


// resolves the called class at run time

$class = get_called_class();

if (!isset(self::$_instances[$class]))


self::$_instances[$class] = new $class;


return self::$_instances[$class];


// overridden by some subclasses

protected function __construct(){}

// prevent cloning instance of the registry

protected function __clone(){}

// implemented by subclasses

abstract public function set($key, $value);

// implemented by subclasses

abstract public function get($key);

// implemented by subclasses

abstract public function clear();


Considering that this abstract registry was covered in depth in earlier parts of the series, I'm not going to waste your time explaining once again how it works. Instead, I suggest you take a look at the source code of the following session handling class. It inherits the static "getInstance()" method defined by its parent and implements the remaining ones as well. Here's the class in question:



class SessionRegistry extends AbstractRegistry


// protected constructor

protected function __construct()




// serve data to the session registry

public function set($key, $value)


$_SESSION[$key] = $value;


// get session data from the session registry

public function get($key)


return isset($_SESSION[$key]) ? $_SESSION[$key] : NULL;


// clear the state of the session registry

public function clear()







As you can see, building a registry that saves and fetches resources from the $_SESSION superglobal array is an extremely simple process. Aside from implementing the "set()" and "key()" methods declared by the abstract parent, this class implements a couple of additional methods (including the constructor) for creating, restoring and destroying a session.

Of course, if you found it easy to understand the logic driving the earlier registry, then it'll be even simpler for you to grasp how it can be used in a concrete case. Still not convinced? Look at the following script, which shows how to handle some session data using the Singleton instance of the registry:

// include source classes

require_once 'AbstractRegistry.php';

require_once 'SessionRegistry.php';

// get Singleton instance of the SessionRegistry class

$sessionRegistry = SessionRegistry::getInstance();

// save some data to the session registry

$sessionRegistry->set('user2', 'Susan Norton');

// get data from the session registry

echo 'Full name of user2 : ' . $sessionRegistry->get('user2');

There you have it. As the above code snippet depicts, the registry pattern can be used in multiple cases, including the implementation of a basic, yet functional session handler. As I stated in the introduction, though, in this last chapter of the series I'd like to demonstrate how to use all of the registries defined so far at the same time. Since I want to save myself from the hassle of manually loading those classes via PHP includes, in the following segment I'm going to create an autoloader, which as you'll see in a moment, will be very easy to understand.

Now, click on the link that appears below 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: