Home arrow PHP arrow Page 4 - Registry Design Pattern: Complete Implementation

Using the registry classes in a single script - PHP

Welcome to the conclusion of a three-part article series that shows you how to implement the Registry design pattern in PHP. In this part, we'll take all the classes that we've created so far and put them to work together.

  1. Registry Design Pattern: Complete Implementation
  2. Review: building a basic session-based registry
  3. Defining a basic autoloader class
  4. Using the registry classes in a single script
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
July 21, 2010

print this article



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

require_once 'Autoloader.php';

// 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)


echo $e->getMessage();



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.

Final thoughts

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!

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: