Re: DB singleton function?

From: Date: Wed, 27 Oct 2004 02:12:16 +0000
Subject: Re: DB singleton function?
References: 1 2  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-34097@lists.php.net to get a copy of this message
Have a look at the bug database for DB package... http://pear.php.net/bugs/bug.php?id=669 -Philippe Ian Eure wrote:
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];
}


« previous php.pear.dev (#34097) next »