Skip to main content
Topic: Database/singleton/functions (Read 6366 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

Database/singleton/functions

At the moment we have:
Code: (Db-something.class.php) [Select]
	private static $_db = null;
[...]
/**
* Returns a reference to the existing instance
*/
static function db()
{
return self::$_db;
}

Then:
Code: (Database.subs.php) [Select]
/**
 * Retrieve existing instance of the active database class.
 *
 * @return Database
 */
function database()
{
global $db_type;

// quick 'n dirty retrieval
if ($db_type == 'mysql')
$db = Database_MySQL::db();
elseif ($db_type == 'postgresql')
$db = Database_PostgreSQL::db();
elseif ($db_type == 'sqlite')
$db = Database_SQLite::db();

return $db;
}

In other words, a singleton class called from a function.

I was wondering, why not have a "proper" class and a function with a static variable?
In other words, remove the singleton part from the class and change to:
Code: (Database.subs.php) [Select]
/**
 * Retrieve existing instance of the active database class.
 *
 * @return Database
 */
function database()
{
global $db_type;
static $db;

if (isset($db))
return $db;

// quick 'n dirty retrieval
if ($db_type == 'mysql')
$db = new Database_MySQL;
elseif ($db_type == 'postgresql')
$db = new Database_PostgreSQL;
elseif ($db_type == 'sqlite')
$db = new Database_SQLite;

$db->initiate();

return $db;
}

Or something like that.

Any php or software design expert around? O:-)
Bugs creator.
Features destroyer.
Template killer.


Re: Database/singleton/functions

Reply #2

makes sense (and no, I'm not a php expert).
Thorsten "TE" Eurich
------------------------

Re: Database/singleton/functions

Reply #3

No expert here either ... but to me it seems less obvious that the class is a singleton if done that way .. I'm not sure I like the way that static $db and $tbl are defined.  To me it just seems more tidy if the class takes care of itself.

Today it just does this (well with that function call in there since we have to call the right class) and its not called instance but I put that there so it was more clear to me.

Code: [Select]
class DataBase
{
private static $instance = null;

public static function getInstance()
{
if (is_null(self::$instance))
self::$instance = new self();

return self::$instance;
}
}

$db = DataBase::getInstance();

Re: Database/singleton/functions

Reply #4

Yep, because in fact it is not a singleton.

The main reason I was thinking at this is because of the fact that almost any method of the class has a parameter you can use to override the connection (except it works only for mysql), but in that case you have to initiate the new connection all by yourself, because otherwise, using initiate, the class is "re-initialized" completely and all the old connection data are wiped out.

Instead, with this method, you should be able to re-instantiate the class with another connection without touching the current one...I think.
Bugs creator.
Features destroyer.
Template killer.