3 Replies - 1507 Views - Last Post: 10 May 2011 - 04:33 PM Rate Topic: -----

#1 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 782
  • View blog
  • Posts: 1,662
  • Joined: 30-January 09

Best method for a singleton DB class

Posted 09 May 2011 - 08:52 PM

Just wondering if I can get some feedback on how to write a singleton DB class so that other classes built on that don't open up multiple connections, just open up the one connection.

Given the follownig base data connector:
abstract class BaseDataObject
{
	// Declare properties
	private static $dbh;
	
	// Establish database connection
	protected static function getDb()
	{
		static $dbh = null;
		if (is_null($dbh))
		{
			$dsn = DBMS . ':host=' . DBHOST . ';dbname=' . DBNAME;
			$dbh = new PDO($dsn, DBUSER, DBPASS);
			$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		}
		return $dbh;
	}
}



...which method will ensure that there is a singleton connection?...

Extend?
class MyConnection1 extends BaseDataObject
{
	public function queryDB($sql)
	{
		$sth = $this->getDb()->prepare($sql);
		$sth->execute();

		// Continue query processing...
	}		
}



Static Call?
class MyConnection2
{
	public function queryDB($sql)
	{
		$sth = BaseDataObject::getDb()->prepare($sql);
		$sth->execute();

		// Continue query processing...
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Best method for a singleton DB class

#2 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 782
  • View blog
  • Posts: 1,662
  • Joined: 30-January 09

Re: Best method for a singleton DB class

Posted 09 May 2011 - 09:11 PM

P.S. I am aware of Dormilich's tutorial on the singleton DB class, mainly just wondering if the call to the DB object needs to be static or if it can be an extension.
Was This Post Helpful? 0
  • +
  • -

#3 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3390
  • View blog
  • Posts: 9,577
  • Joined: 08-June 10

Re: Best method for a singleton DB class

Posted 09 May 2011 - 10:29 PM

what do you mean by "extension"? usually it doesnít make sense to extend the connection singleton, because the singleton is only supposed to provide the object. thus you donít need to extend it.

regarding the code, either you use static (line #9) or the class property (line #4).

if you want to use the extended version, you donít need a static call (but that defeats the Singleton as is).
class Base
{
    protected static $dbh;

    public function connect()
    {
        if (NULL === self::$dbh)
        {
        // etc.
        }
    } 
}

class Query extends Base
{
    public function __construct()
    {
        $this->connect();
    }

    public function query($sql)
    {
        parent::$dbh->prepare($sql);
        // etc.
    }
}

Was This Post Helpful? 1
  • +
  • -

#4 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 782
  • View blog
  • Posts: 1,662
  • Joined: 30-January 09

Re: Best method for a singleton DB class

Posted 10 May 2011 - 04:33 PM

View PostDormilich, on 09 May 2011 - 11:29 PM, said:

what do you mean by "extension"? usually it doesnít make sense to extend the connection singleton, because the singleton is only supposed to provide the object. thus you donít need to extend it.

Just the answer I needed, thanks again dormi
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1