Page 96      All Pages  All Books
96 The Registry Pattern
return RegistryMonoState::$store[$key]; }
function set($key, $obj) {
RegistryMonoState::$store[$key] = $obj; } }
An interesting side effect of coding the Registry in PHP5 this way is you can actually use both instance and static method calls with the same set of code. Here is a test case that proves that—it uses static method calls only.
class RegistryMonoStatePHP5TestCase extends UnitTestCase { function testRegistryMonoState() { /*...*/ }
function testRegistryMonoStateStaticCalls() {
$this->assertFalse(RegistryMonoState::isValid(‘key’)); $this->assertNull(RegistryMonoState::get(‘key’)); $test_value = new TestObj; RegistryMonoState::set(‘key’, $test_value); $this->assertIdentical($test_value, RegistryMonoState::get(‘key’)); }
Now that you’ve seen how the static call interface looks in PHP5, let’s code the same interface in PHP4. As in the previous PHP4 “static class variable” emulation, this implementation needs to use the “function static returning a reference” trick.
The test for PHP4 static call interface looks similar to the PHP5 version of the test.
// PHP4
class RegistryStaticPHP4TestCase extends UnitTestCase {
function testRegistryStatic() {
$this->assertFalse(RegistryStatic::isValid(‘key’)); $this->assertNull(RegistryStatic::get(‘key’)); $test_value = ‘something’; RegistryStatic::set(‘key’, $test_value);
$this->assertReference($test_value, RegistryStatic::get(‘key’)); } }
And here is an implementation that satisfies the test:

Page 96      All Pages  All Books