SitePoint Sponsor |
|
User Tag List
Results 1 to 5 of 5
-
Jan 3, 2005, 08:54 #1
- Join Date
- Nov 2004
- Location
- Ithaca, NY
- Posts
- 69
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Registry class -- will this work?
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:
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. }
-
Jan 3, 2005, 09:45 #2
- Join Date
- Jun 2003
- Location
- Iowa, USA
- Posts
- 3,749
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
It looks like your "get()" method is a mixture between a getKey() and a getInstance() that I would avoid.
Also, with PHP4 you always want to catch the objects by reference:
PHP Code:$database =& Registry::get('database');
$session =& Registry::get('session');
And, lastly, PHP4 does not store references in static variables. See my post #4 in this thread for more details.
HTHJason Sweat ZCE - jsweat_php@yahoo.com
Book: PHP Patterns
Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
Detestable (adjective): software that isn't testable.
-
Jan 4, 2005, 07:53 #3
- Join Date
- Jan 2005
- Location
- United Kingdom
- Posts
- 208
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
A did a similar thing with PHP 5 (didn't actually know there was a design pattern for this). Anyway, in PHP 5 you can take advantage of static member variables which makes the whole thing alot easier. Things are a bit more tricky with PHP 4. Hope it helps a bit.
PHP Code:abstract class Repository
{
static private $repository = array();
/**
* Returns a static instance of $class
* @param string $class
* @return obj $$class
*/
final static function getInstance($class, $arg = NULL)
{
static $repository;
if(is_array($arg))
{
$arg = implode(",", $arg);
}
if(!isset(Repository::$repository[$class]))
{
Repository::$repository[$class] = new $class($arg);
}
return Repository::$repository[$class];
}
}
// Requests the current Singleton, or creates a new one.
Repository::getInstance("SqlQueryHandler");
-
Jan 4, 2005, 08:43 #4
- Join Date
- Jun 2004
- Location
- Copenhagen, Denmark
- Posts
- 6,157
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You may want to search theese forums for Singleton
-
Jan 4, 2005, 15:06 #5
- Join Date
- Oct 2004
- Location
- Kansas City, MO
- Posts
- 68
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I would suggest taking a look at phpPatterns Registry article. That should give you a pretty good insight as to what to do. It also has a fully ready to use PHP4 Registry class as part of the examples.
Bookmarks