فصل 25. Zend_Registry

قائمة المحتويات

25.1. Using the Registry
25.1.1. Setting Values in the Registry
25.1.2. Getting Values from the Registry
25.1.3. Constructing a Registry Object
25.1.4. Accessing the Registry as an Array
25.1.5. Accessing the Registry as an Object
25.1.6. Querying if an index exists
25.1.7. Extending the Registry
25.1.8. Unsetting the Static Registry

25.1. Using the Registry

The registry is a container for storing objects and values in the application space. By storing the value in the registry, the same object is always available throughout your application. This mechanism is an alternative to using global storage.

The typical usage of the registry is through static methods in the Zend_Registry class. Alternatively, the class is an array object, so you can access elements stored within it with a convenient array-like interface.

25.1.1. Setting Values in the Registry

To store an entry in the registry, use the static method set().

مثال 25.1. Example of set() method

<?php

Zend_Registry::set('index', $value);

?>

The value can be an object, an array, or a scalar. You can change the value stored in a specific entry of the registry by using set() to set it to a new value.

The index can be a scalar, either string or integer, like an ordinary array.

25.1.2. Getting Values from the Registry

To retrieve an entry from the registry, use the static method get().

مثال 25.2. Example of get() method

<?php

$value = Zend_Registry::get('index');

?>

The getInstance() method returns the static registry object.

A registry object is iterable.

مثال 25.3. Example of iterating over the registry

<?php

$registry = Zend_Registry::getInstance();

foreach ($registry as $index => $value) {
    echo "Registry index $index contains:\n";
    var_dump($value);
}

?>

25.1.3. Constructing a Registry Object

In addition to accessing the static registry through static methods, you can create an instance directly and use it as an object.

The registry instance you access through the static methods is simply one such instance, and it is for convenience that it is stored statically, so you can access it from anywhere in your appliation.

Use a traditional new constructor to create an instance of the registry. This gives you the opportunity to initialize the entries in the registry as an associatve array.

مثال 25.4. Example of constructing a registry

<?php

$registry = new Zend_Registry(array('index' => $value));

?>

After constructing this instance, you can use it using array-object methods, or you can set this instance to become the static instance using the static method setInstance().

مثال 25.5. Example of initializing the static registry

<?php

$registry = new Zend_Registry(array('index' => $value));

Zend_Registry::setInstance($registry);

?>

The setInstance() method throws a Zend_Exception if the static registry has already been initialized by its first access.

25.1.4. Accessing the Registry as an Array

If you have several values to get or set, you may find it convenient to access the registry with array notation.

مثال 25.6. Example of array access

<?php

$registry = Zend_Registry::getInstance();

$registry['index'] = $value;

var_dump( $registry['index'] );

?>

25.1.5. Accessing the Registry as an Object

You may also find it convenient to access the registry in an object-oriented fashion, using index names as object properties. To do this, you need to specifically construct the registry object using the ArrayObject::ARRAY_AS_PROPS option, and initialize the static instance. You must do this before the static registry has been accessed for the first time. Beware of using this option, since some versions of PHP have bugs when using the registry with this option.

مثال 25.7. Example of object access

<?php

// in your application bootstrap:
$registry = new Zend_Registry(array(), ArrayObject::ARRAY_AS_PROPS)
Zend_Registry::setInstance($registry);
$registry->tree = 'apple';

.
.
.

// in a different function, elsewhere in your application:
$registry = Zend_Registry::getInstance();

echo $registry->tree; // echo's "apple"

$registry->index = $value;

var_dump($registry->index);

?>

25.1.6. Querying if an index exists

To find out if a particular index in the registry has a value, use the static method isRegistered().

مثال 25.8. Example of isRegistered() method

<?php

if (Zend_Registry::isRegistered($index)) {
    $value = Zend_Registry::get($index);
}

?>

To find out if a particular index in a registry array-object has a value, use isset() like you would with an ordinary array.

مثال 25.9. Example of isset() method

<?php

$registry = Zend_Registry::getInstance();

// using array-access syntax
if (isset($registry['index'])) {
    var_dump( $registry['index'] );
}

// using object-access syntax, if enabled
if (isset($registry->index)) {
    var_dump( $registry->index );
}

?>

25.1.7. Extending the Registry

The static registry is an instance of the class Zend_Registry. If you want to add functionality to the registry, you can create a class that extends Zend_Registry, and then you can specify this class as the class to use for the static registry. Use the static method setClassName() to specify the class. The class must extend Zend_Registry.

مثال 25.10. Example of specifying the static registry's class name

<?php

Zend_Registry::setClassName('My_Registry');

Zend_Registry::set('index', $value);

?>

The registry throws a Zend_Exception if you try to set the classname after the registry has been accessed for the first time. It is recommended that you specify the classname for your static registry in your application bootstrap.

25.1.8. Unsetting the Static Registry

Although it is not normally necessary, you can unset the static instance of the registry. Use the static method _unsetInstance().

[ملاحظة] Data loss risk

When you use _unsetInstance(), all data in the static registry are discarded and cannot be recovered.

You might use this method, for example, if you want to use setInstance() or setClassName() after the static registry object has been initialized. Unsetting the static instance allows you to use these methods.

مثال 25.11. Example of _unsetInstance() method

<?php

Zend_Registry::set('index', $value);

Zend_Registry::_unsetInstance();

// change the class
Zend_Registry::setClassName('My_Registry');

Zend_Registry::set('index', $value);

?>