HomePHP 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.
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!
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!