Registry Pattern in Zend Framework

The registry has provided a mechanism which help you to store and maintain your objects within the application. It’s aim at preventing improperly using the global objects by users.

 

The registry usually provide the functions which makes you really comfortable to manage the objects such as storing, fetching, deleting and so on.

 

Now let’s take a look at how Zend_Registry works in Zend Framework :

 

 

[codesyntax lang=”php”]

class Zend_Registry extends ArrayObject
{
    ......

    // Registry object provides storage for shared objects.
    private static $_registry = null;

    // Constructor : create as the ArrayObject with default
    // @link http://www.php.net/manual/en/class.arrayobject.php
    public function __construct($array = array(), $flags = parent::ARRAY_AS_PROPS)
    {
        parent::__construct($array, $flags);
    }

    // Retrieves the default registry instance.
    public static function getInstance()
    {
        if (self::$_registry === null) {
            self::$_registry = new Zend_Registry();
        }

        return self::$_registry;
    }

    // Unset the default registry instance.
    public static function _unsetInstance()
    {
        self::$_registry = null;
    }

    // Getter method, based on offsetGet(). static instance stored in the registry instance.
    public static function get($index)
    {
        $instance = self::getInstance();
        ...
        return $instance->offsetGet($index);
    }

    // Setter method, based on as offsetSet().
    public static function set($index, $value)
    {
        $instance = self::getInstance();
        $instance->offsetSet($index, $value);
    }

    ...
}

[/codesyntax]

 

The source code above is simplified in order to keep clear. The registry is inherited from php build-in class ArrayObject (http://www.php.net/manual/en/class.arrayobject.php) which means we can see and use Zend_Registry as an array.

 

Here are two ways which are the same in fact to get/set the instance :

 

[codesyntax lang=”php”]

Zend_Registry::set($name, $value);
......
$value2 = Zend_Registry::get($name);

$registry= Zend_Registry::getInstance();
$registry[$name] = $value;
......
$value2 = $registry[$name];

[/codesyntax]

 

In the other hand, the registry is usually concerted with singleton (see "Zend Framework and Design Pattern (1) – singleton"). for example :

 

[codesyntax lang=”php”]

class singletonExample
{
    ......

    // Singleton instance
    public static function getInstance()
    {
        if (false == Zend_Registry::isRegistered('singletonExample')) {
            Zend_Registry::set('singletonExample', new self());
        }

        return Zend_Registry::get('singletonExample');
    }

    ......
}

[/codesyntax]

 

The Zend_Registry has also became a component provider in Zend Framework. For example, Zend_Registry has provided the default translator to Zend_View_Helper_Translate :

 

 

[codesyntax lang=”php”]

class Zend_View_Helper_Translate extends Zend_View_Helper_Abstract
{
    ...

    // Retrieve translation object.
    // If none registered, try to pull it from the registry
    // using the key 'Zend_Translate'.
    public function getTranslator()
    {
        if ($this->_translator === null) {
            require_once 'Zend/Registry.php';
            if (Zend_Registry::isRegistered('Zend_Translate') === true) {
                $this->setTranslator(Zend_Registry::get('Zend_Translate'));
            }
        }

        return $this->_translator;
    }

    ...
}

[/codesyntax]

 

In one word, the registry has been the global objects manager for the application. It’s one of the most basic and widely used pattern and therefore the indispensability component in the framework.

 

Posted in Design Patterns | Tagged , | Leave a comment