Page 94      All Pages  All Books
94 The Registry Pattern
The real magic in this alternative is the line $this->_store =& $GLOBALS[REGISTRY_GLOBAL_STORE;], where the reference operator binds the global array to the instance variable $_store. This is the key to MonoState implementations: each time $this->_store is used in the object, the actual effect is mirrored to the global variable.
But it hardly makes sense to recommend a solution based on global variables. A static class vari-able would be a better solution, if only PHP4 provided such a feature. Yet, is there a way to use refer-ences to implement a static class variable in your own code?
The tests can be similar to the RegistryGlobal tests:
class RegistryMonoStatePHP4TestCase extends UnitTestCase { function testRegistryMonoState() { $this->assertCopy(
$reg =& new RegistryMonoState ,$reg2 =& new RegistryMonoState); $this->assertFalse($reg->isValid(‘key’)); $this->assertNull($reg->get(‘key’)); $test_value = ‘something’; $reg->set(‘key’, $test_value);
$this->assertReference($reg->get(‘key’), $reg2->get(‘key’)); } }
To make your own class static variable, bind a reference to a function static variable to a class instance variable.
class RegistryMonoState { var $_store;
function  &_initRegistry() {
static   $store = array();
return   $store; }
function RegistryMonoState() {
$this->_store =& $this->_initRegistry(); }
function isValid($key) {
return array_key_exists($key, $this->_store); }
function &get($key) {
if (array_key_exists($key, $this->_store)) return $this->_store[$key]; }

Page 94      All Pages  All Books