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

The session-driven registry in action - 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

Below you'll find an example that shows how to work with the session registry class that you saw before. Here it is:

// 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');

Mission accomplished. As you can see, storing and retrieving resources by using the Singleton instance of the previous "SessionRegistry" class is a straightforward process that can be grasped in a snap. Again, for demonstration purposes the class performs the corresponding saving and retrieval tasks on a sample string, but a registry (be it static or functional at instance-level) will most likely be utilized with objects. In either case, the code fragments included in this tutorial should provide you with the right pointers to grasp the underlying logic of building several registries in PHP.

Lastly, if you feel adventurous and want to expand your current programming skills, I suggest you use Composition instead of Inheritance and create registries that utilize different storage mechanisms, which can be injected into a class that consumes a common interface. In doing so, you'll be taking advantage of the benefits offered by Polymorphism. What are you waiting for? Go ahead and start coding!

Final thoughts

Over the course of this second part of the series, I demonstrated how easy it is to define a session-based registry class based on the abstract parent created in the first tutorial. This creation process was extremely straightforward, so in theory you shouldn't have major problems building your own registry classes or even improving the ones that you learned so far in this series.

In the last article, I'm going to set up a final example that will put all of these sample registry classes to work together. Now that you know what to expect from that final installment, you don't have any excuses to miss it!



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