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.
blog comments powered by Disqus |
|
|
|
|
|
|
|