As usual, before I begin explaining how to create the session-based registry class mentioned in the introduction, first I'd like to spend a few moments reintroducing the source code of the classes defined in the previous part of the series. It used the "get_called_class()" function available from PHP 5.3 to build a hierarchy of Singleton registries. Here's how the classes in question were initially created, starting with the corresponding abstract parent: (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(); } As you can see above, understanding the driving logic of the previous abstract registry is a straightforward process. All that this class does is implement its Singleton "getInstance()" method and declare the remaining ones abstract (with the exception of the pertinent constructor, naturally). With this abstract parent already up and running, creating a concrete registry that saves and fetches resources from an internal array is as simple as creating the following subclass. Take a look at it, please: (ArrayRegistry.php) <?php class ArrayRegistry extends AbstractRegistry { private $_data = array(); // save new data to the array registry public function set($key, $value) { $this->_data[$key] = $value; } // get data from the array registry public function get($key) { return isset($this->_data[$key]) ? $this->_data[$key] : NULL; } // clear the state of the array registry public function clear() { $this->_data = array(); } } There you have it. While the new "ArrayRegistry" class only implements three basic and easy-to-follow methods, it encapsulates the functionality required for handling resources (be it strings, numbers or objects) throughout different stages of an application via a single private array. That's not rocket science, is it? If you're interested in seeing how to put this class to work, take a look at the following example. It shows how to use the class to store the full name of an old, famous writer to the class's $_data array, and how to retrieve it at a later time. Here's how the example in question looks: // include source classes require_once 'AbstractRegistry.php'; require_once 'ArrayRegistry.php'; // get Singleton instance of the ArrayRegistry class $arrayRegistry = ArrayRegistry::getInstance(); // save some data to the array registry at one point of the application $arrayRegistry->set('user1', 'Robert Louis Stevenson'); // get data from the array registry at some other point of the application echo 'Full name of user1 : ' . $arrayRegistry->get('user1'); See how easy it is to use this array-based registry? I bet you do! To make the above example a little more educational and "literary," the resource being saved and fetched afterward is a string containing the author of the classic terror novel "The Strange Case of Dr. Jekyll and Mr. Hyde" (http://en.wikipedia.org/wiki/Strange_Case_of_Dr_Jekyll_and_Mr_Hyde), (which both scared and fascinated me in my childhood). It's possible to handle other kinds of data, such as numbers, arrays and even objects. So far, so good. Now that you've recalled how to work with the previous registry classes, it's time to continue exploring the functionality offered by this pattern. Therefore, in the next section I'm going to demonstrate how to create a whole new registry class whose underlying storage mechanism will be the superglobal $_SESSION PHP array. To learn more about how this session-based registry will be defined, click on the link that appears below and keep on reading.
blog comments powered by Disqus |
|
|
|
|
|
|
|