I'm pretty new to the whole idea of designing with patterns, so please correct me if I start to sound clueless here. But it seems that my application calls for a singleton registry, because I'll be using a single global instance of a few different objects, and thanks to help here I think I've got a way to do it. I'm just wondering if anyone sees obvious reasons why this won't work, or if it throws up any red flags or anything...
Here's Registry.php:
Then, in my other classes, I'd call it something like this:Code:<? class Registry { var $cache; function Registry() { $this->cache = array(); } function setEntry($key, &$item) { $this->cache[$key] = &$item; } function isEntry($key) { return ($this->getEntry($key) !== null); function &get($key) { static $registry; if ($registry && $key) { return $registry->cache[$key]; } else { $registry = new Registry(); return $registry; } } } $r = Registry::get(); $r->setEntry('database', new DatabaseObject()); $r->setEntry('session', new SessionObject()); ?>
Code:function doSomething(){ $database = Registry::get('database'); $session = Registry::get('session'); // Do stuff with them. }





Bookmarks