As I said in the segment that you just read, the only thing that remains undone is creating a script that uses all of the registry classes defined so far at the same time. This is exactly what the code fragment below does, so pay attention to it, please:
// include autoloader
// get instance of the autoloader
// get Singleton instance of the ArrayRegistry class
$arrayRegistry = ArrayRegistry::getInstance();
// save some data to the array registry
$arrayRegistry->set('user1', 'Linda Smith');
// get data from the array registry
echo 'Full name of user1 : ' . $arrayRegistry->get('user1');
// 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');
// catch exceptions
catch (Exception $e)
Undeniably, the above script is very easy to understand. It first grabs the instance of the autoloader, and then uses each registry to save and retrieve the full names of a couple of fictional users. Even though all the registry classes shown in this example are Singletons, it doesn't mean that all of the registries have to be created that way all the time. Keep this concept in mind, especially if you're planning to build your own registries.
Finally, feel free to tweak all of the code samples developed in this series. This process will surely provide you with a more intimate background in implementing the registry design pattern in PHP.
Sad but true, we've come to the end of this series. As you saw, the implementation of registry classes in PHP is actually a straightforward process that can be mastered with minor effort, even if you're just taking your first steps in the field of object-oriented programming.
By far, the most controversial facet of the pattern is that it provides global access to resources within an application, which is not a desirable feature in all cases. So let me make a simple and pragmatic suggestion: use the pattern with due caution and responsibility.
See you in the next PHP development tutorial!
blog comments powered by Disqus