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