Zend_Registry - do you need getInstance ()?
Hey, I'm wondering when you access Zend_Registry in your application, if you need to enable getInstance (), and if so, why?
eg
Zend_Registry::getInstance()->get('db');
against.
Zend_Registry::get('db');
they both seem to work with the later, less verbose one. I am vaguely aware that Zend_Registry is a singleton, which I think means there can only be one instance of it? so why do you need getInstance ()?
a source to share
I wrote the code for Zend_Registry in 2007.
You can use static get()
and set()
, and this affects the default instance of the registry, which is stored as a static member of the class. This is for most situations.
All of the other Zend_Registry methods support pretty much non-standard situations, for example if you want to implement your own class for your registry, but still make it standalone. Or, if you want to get a real registry object, for example, so that you can serialize the entire registry object with all its contents.
a source to share
You don't need to use getInstance()
. If you look into the class Zend_Registry
, you will see:
public static function get($index)
{
$instance = self::getInstance();
if (!$instance->offsetExists($index)) {
require_once 'Zend/Exception.php';
throw new Zend_Exception("No entry is registered for key '$index'");
}
return $instance->offsetGet($index);
}
It gets an instance for you in static methods.
a source to share
You might want to use Zend_Registry :: getInstance () if you want to access the number of calls to the registry; instead of going through the offsetExists () method and subsequent calls to array_key_exists () for each property, you get an instance and then step through the ArrayObject methods to get the saved entries.
Another reason why you might want to use Zend_Registry :: getInstance () is if you keep the registry in a cache.
Other than that, you can find Zend_Registry :: get () for whatever you need.
a source to share