Home arrow PHP arrow Page 3 - Session Handler with the Registry Design Pattern

Building a session-based registry - PHP

In this second part of a series, I demonstrate how easy it is to define a session-based registry class based on the abstract parent created in the first tutorial. The straightforward creation process means you shouldn’t have major problems building your own registry classes or even improving the ones that you've learned so far in this series.

TABLE OF CONTENTS:
  1. Session Handler with the Registry Design Pattern
  2. Review: building an array-based registry
  3. Building a session-based registry
  4. The session-driven registry in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
July 19, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

In reality, building a session-driven registry is very similar to creating one that handles resources via arrays, except for some tiny subtleties that are easy to guess: first, the registry must be capable of creating and destroying sessions through specific methods; and second, all of the resources must be stored on the familiar $_SESSION PHP array.

With that said, here's the definition of this whole new registry class, which fits the aforementioned requirements in a pretty decent way:

(SessionRegistry.php)

<?php

class SessionRegistry extends AbstractRegistry

{

// protected constructor

protected function __construct()

{

session_start();

}

 

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

{

session_start();

session_unset();

session_destroy();

}

}

As I explained a moment ago, creating a registry that saves and fetches session data is an approachable process that doesn't bear any further discussion. In the class above, the constructor is responsible for creating/resuming a session, while the "clear()" method is charged with destroying it by using some session-related PHP functions, which you've surely used hundreds of times before.

Disguised under the rather obscure name of "session-based registry," the previous class is nothing but a simple (yet functional) session handling class that allows you to manipulate session data via a couple of setters and getters, and nothing else.

So, do you understand how this registry does its thing? Great. Now, it's time to leap forward and give it a try, so you can see for yourself if it works as expected. This testing process will be accomplished in the following section, so go ahead and read the lines to come.



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