Índice
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.
To store an entry in the registry, use the static method
set()
.
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.
To retrieve an entry from the registry, use the static method
get()
.
The getInstance()
method returns the static registry object.
A registry object is iterable.
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.
Exemplo 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()
.
Exemplo 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.
If you have several values to get or set, you may find it convenient to access the registry with array notation.
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.
Exemplo 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); ?>
To find out if a particular index in the registry
has a value, use the static method isRegistered()
.
Exemplo 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.
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.
Exemplo 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.
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 |
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.