17. Let??™s examine it.
Listing 17.17 The Registry as implemented by Zend_Registry
class Zend_Registry extends ArrayObject
{
public static function getInstance() 1
{
if (self::$_registry === null) {
self::init();
}
return self::$_registry;
}
public static function set($index, $value)
{
$instance = self::getInstance();
$instance->offsetSet($index, $value); 2
}
public static function get($index)
{
$instance = self::getInstance();
if (!$instance->offsetExists($index)) { A
require_once 'Zend/Exception.php'; A
throw new Zend_Exception("No entry is [CA] A
registered for key '$index'"); A
}
return $instance->offsetGet($index); 3
}
} 1 Internally, a Singleton is used
2 Set element via ArrayObject insterface
A Check that key exists in ArrayObject
3 Retrieve and return
Internally, the Zend_Registry uses a Singleton-like function, getInstance() to retrieve an instance of itself
(#1). This is used in both get() and set() and ensures that both functions are using the same registry when
setting and retrieving items. Zend_Registry implements ArrayObject which means that you can treat an
instance of it as if it was an array and so iterate over it or directly set and get elements. As such, the class
implements the functions required by the interface, including offsetGet(), offsetSet() and offsetExists() which
are the same as the ArrayAccess functions we looked at in section 17.
Pages:
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379