Home arrow PHP arrow Page 3 - Registry Design Pattern

Building an array-based registry - PHP

In this first part of a series, I provide a basic introduction to implementing the registry design pattern in PHP. I'll define a simple hierarchy of classes comprised of an abstract registry, and a concrete implementation of it, which uses a private array to save and fetch resources across different points of an application.

  1. Registry Design Pattern
  2. Defining an abstract registry class
  3. Building an array-based registry
  4. Putting the array-based registry to work
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
July 14, 2010

print this article



To be frank, building a Singleton array-based registry is indeed a straightforward process that can be mastered with minor hassles. Still not convinced of this? Well, in that case take a look at the definition of the following class, which hopefully will make your doubts vanish. Here it is:






class ArrayRegistry extends AbstractRegistry


    private $_data = array();


    // save new data to the array registry

    public function set($key, $value)


        $this->_data[$key] = $value;



    // get data from the array registry

    public function get($key)


        return isset($this->_data[$key]) ? $this->_data[$key] : NULL;



    // clear the state of the array registry

    public function clear()


        $this->_data = array();



There you have it. As you can see, creating a class that stores and fetches resources from an array is a painless task, one that you may have done many times before. In this case, this concrete registry not only implements the abstract methods declared by its corresponding parent, but it also inherits from it the “getInstance()” Singleton, thanks to the functionality of Late Static Bindings. That was really simple to grasp, wasn’t it?

If you’re planning to build a registry that fits more specific requirements, the one shown above should be enhanced accordingly by adding to it some constraints, especially within its “set()” method. For instance, you might want to make the registry only suitable for storing/retrieving objects; in a case like this, make sure to include the appropriate validation code into this method, and everything should work just fine.

Well, now that we have a concrete registry available for testing purposes, it’s time to give it a try and see if it really works as expected. So, in the next segment I’m going to set up an example that will show the registry’s functionality in a few simple steps.

To learn more about how this example will be developed, jump forward and read the lines to come.

>>> 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: