Re: DB singleton function?

From: Date: Tue, 26 Oct 2004 21:15:08 +0000
Subject: Re: DB singleton function?
References: 1  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-34086@lists.php.net to get a copy of this message
On Tuesday 26 October 2004 02:02 pm, Joe Stump wrote: > Why isn't there a DB singleton function like there is in Log? It would be > extremely simple to implement and could be used when PHP5 finally becomes > standard. > > Ie. > > function &singleton ($dsn) > { > if (!isset($GLOBALS['_DB_Connections'][$dsn])) { > $GLOBALS['_DB_Connections'][$dsn] = & DB::connect($dsn); > } > > return $GLOBALS['_DB_Connections'][$dsn]; > } > I was wondering this a while back, and I was going to work something up. I wasn't sure if singleton() should try to connect(), or just call factory(). Connecting seems like the right thing, but I think there's an assumption that singleton() will work like factory(), e.g. not connect. And connectSingleton() seemed like a pretty bad function name. Any DB developers have feedback on this issue? btw, you should use static vars instead of globals: function ... { static $instances; if (!isset($instances[$dsn]) || DB::isError($instances[$dsn]) { ...; } return $instances[$dsn]; }

Attachment: [application/pgp-signature]
« previous php.pear.dev (#34086) next »