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: (AbstractRegistry.php) <?php 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: (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 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.
blog comments powered by Disqus |
|
|
|
|
|
|
|