jump to navigation

Zend Framework : Using the Zend_Registry component April 9, 2011

Posted by Tournas Dimitrios in Zend Framework v1.10.

The Zend Framework has a component called the Zend_Registry which allows you to store data into a sort of global space which lasts the duration of the script. Once saved to the registry the data is available throughout the application. This post shows how to save some data to the registry and then retrieve it.

You don’t need to create a Zend_Registry object and can simply access it via its static methods. Alternatively, the registry can be used as an array object, so you can access elements stored within it with a convenient array-like interface.

  • Saving and retrieving a scalar value :  To save a value to the registry use the set() method as follows:
    Zend_Registry::set(‘foo’, $value) ;  // Setting the value
    $var = Zend_Registry::get(‘foo’) ;  //Retrieving the value
  • Saving and retrieving an array : This is the same as for saving a regular value above. A copy of the array is made to the registry and subsequent modifications to the variable do not affect the registry unless saved back to it again. Object handling is different and is covered in the next section.
    $value = array(1, 2, 3);
    Zend_Registry::set(‘foo’, $value);
    $value[] = 4;
    $someval = Zend_Registry::get(‘foo’);
    print_r($someval) ; //prints 1 , 2 , 3 i.e. the new element ‘4’ in $value has not been added to the array in the registry.
  • Saving and retreiving an object : When saving an object to the registry, it is a reference to the object that is saved. Subsequent changes to the original object therefore affect the copy in the registry.
    //For example if we had the following class:
    class foo {
        var $a;
        var $b;
        var $c;
    //And did the following:
    $value = new foo();
    Zend_Registry::set('foo', $value);
    $value->a = 1;
    $value->b = 2;
    $value->c = 3;
    //The output would be:
    foo Object
        [a] => 1
        [b] => 2
        [c] => 3

This is despite the assignments to ->a ->b and ->c being after the object has been saved to the registry.Adding objects such as a session object to the registry can be a really useful way of passing information around, because all you need to do is get the object from the registry and then call functions and assign variables and they will be accessible from anywhere else in your code that gets the object from the registry.
Practical example :
Appending the following code in the application’s public index file :

require_once 'Zend/Registry.php';
Zend_Registry::set('value1', "testvalue1");
Zend_Registry::set('value2', "testvalue2");
* Accessing the values from the application's code
echo Zend_Registry::get('value1');
echo Zend_Registry::get('value2');

//  Transfer the value from the action to the view
<pre>public function indexAction()
    $title = Zend_Registry::get('value1');
    $this->view->assign('name', 'your-name');
    $this->view->assign('RegistyValue', $title);


No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s