I found a db adapter on php.net, on the mysql-connect page.
This is a page viewed quite a lot (I would imagine).
It includes what the author claims is a singleton pattern class.
Found on http://php.net/manual/en/function.mysql-connect.php
Writer says:
“Here’s a singleton class to manage a single Database connection. The
Open method uses constant values as defautls, read from an ini file
through an initializer script loaded at the top of each web page. You
can also override the default values by manually entering your own (in
case you need to switch servers or database names mid-script). The qry
function takes a prepared statement and will return the first row,
first associative row, first cell, or entire result set based on the
second parameter (entire result if omitted).”Usage: $DB = DB::Open();
$result = $DB->qry(” {SQL Statement} ;”);
<?php
abstract class Database_Object
{
protected static $DB_Name;
protected static $DB_Open;
protected static $DB_Conn;
protected function __construct($database, $hostname, $hostport, $username, $password)
{
self::$DB_Name = $database;
self::$DB_Conn = mysql_connect($hostname . ":" . $hostport, $username, $password);
if (!self::$DB_Conn) { die('Critical Stop Error: Database Error<br />' . mysql_error()); }
mysql_select_db(self::$DB_Name, self::$DB_Conn);
}
private function __clone() {}
public function __destruct()
{
// mysql_close(self::$DB_Conn); <-- commented out due to current shared-link close 'feature'. If left in, causes a warning that this is not a valid link resource.
}
}
final class DB extends Database_Object
{
public static function Open($database = DB_NAME, $hostname = DB_HOST, $hostport = DB_PORT, $username = DB_USER,$password = DB_PASS)
{
if (!self::$DB_Open)
{
self::$DB_Open = new self($database, $hostname, $hostport, $username, $password);
}
else
{
self::$DB_Open = null;
self::$DB_Open = new self($database, $hostname, $hostport, $username, $password);
}
return self::$DB_Open;
}
public function qry($sql, $return_format = 0)
{
$query = mysql_query($sql, self::$DB_Conn) OR die(mysql_error());
switch ($return_format)
{
case 1:
$query = mysql_fetch_row($query);
return $query;
break;
case 2:
$query = mysql_fetch_array($query);
return $query;
break;
case 3:
$query = mysql_fetch_row($query);
$query = $query[0];
return $query;
default:
return $query;
}
}
}
?>
After looking at the code I have one question, is this really a singleton (see DB class and Open function)?
What about static properties makes a singleton pattern work ?
I see what you mean, if
self::$DB_OpenDOES exist it sets it to null and creates a new instance of the object anyway.Appears you are right and this does not seem like the singleton pattern.
Regarding singleton pattern with static properties. Unlike instance variables which are in the objects scope, static variables are in class scope.
What this means is that even if you create an object multiple times it will still use the same value for the static property every time. It’s what allows them to be great for counters and therefore global singletons.
See the below:
The above creates 3 instances of x. You can make a change in 1 instance and it will only make changes in that 1 Instance.
But only 1 instance of y is created. So a change in 1 and all 3 will change. They all exist in same context. Therefore point to the same value. If you increment one the others will display the same increment ect..
So to re-iterate, even though only a single instance of
DB_Conndoes exist it continually resets it and writes to it again (it does not have to act like this).To fix the problem the author could return the
self::$DB_Openproperty if it does exist.