Bondenn
Bondenn

Reputation: 1861

Trying to create a database connection with PDO and singleton pattern

I've been trying to create a database connection with PDO and singleton. I think I'm pretty much finished but I'm not sure if I'm using a correct singleton pattern.

I'm also not sure if I'm using the __clone() and the __wakeup() methods correctly. And I don't have the knowledge to test them.

Could anyone please tell me if my approach is on the right path and if I'm using the singleton pattern correctly? I'm very new to design patterns.

Here is my code:

<?php

require_once 'config.php';

class dbConn{

// Variable to store connection object.
protected static $db;

// Assign variables from config.php.
private $host   = DB_HOST;
private $dbuser = DB_USER;
private $dbpass = DB_PASS;
private $dbname = DB_NAME;

// Private construct - class cannot be instatiated externally.
private function __construct() {

    try {
        // Try to create PDO object to the $db variable.
        $pre = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
        self::$db = new PDO($pre, $this->dbuser, $this->dbpass);
        self::$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
    }
    // If not able to connect to database.
    catch (PDOException $e) {
        echo "Could not connect: " . $e->getMessage();
    } 
}

// Get connection function.
public static function getConnection() {

// Only if no connection object exists it creates one, because we only want one instance.
    if (!self::$db) {
// New connection object.
        new dbConn();
}
    return self::$db;
}

public function __clone() {
        return false;
    }

public function __wakeup(){
        return false;
    }
}

$db = dbConn::getConnection()

?>

Upvotes: 0

Views: 1230

Answers (2)

CaSUaL
CaSUaL

Reputation: 154

Just for your know how:

dependency iniection is a pattern like:

class Class {
   private $dependency;
   public function __construct($dep) {
      $this->$dependency = $dep;
   }

   function use_dependency( ... ) {
      $dep = $this->$dependency;
      ...
      $dep->action();
      ...
    }
}

$dep = new Dependency();
$c = new Class( $dep );

the singleton pattern is like:

class Singleton
{
    protected static $instance = null;
    protected function __construct()
    {
        //Thou shalt not construct that which is unconstructable!
    }
    protected function __clone()
    {
        //Me not like clones! Me smash clones!
    }

    public static function getInstance()
    {
        if (!isset(static::$instance)) {
            static::$instance = new static;
        }
        return static::$instance;
    }
}

but i repeat tha is bad practice.

Note also that __wakeup is called after a deserialize (see http://www.php.net/manual/en/language.oop5.magic.php)

Upvotes: 1

CaSUaL
CaSUaL

Reputation: 154

This is not a singleton pattern: a singleton in php is a class that store the unique instance of itself in a static variable.

IMHO is good practice to not use singleton classes, static classed can do the same things.

For what you want to obtain you could see for dependency iniection or

create a stuff like this:

class Database extends PDO {
   private $engine = 'mysql';
   private $host = 'localhost';
   private $database = 'Crumbs';
   private $user = 'root';
   private $pass = 'adminuser';

   public function __construct()
   {
      $dns = $this->engine.':dbname='.$this->database.";host=".$this->host;
      parent::__construct( $dns, $this->user, $this->pass );
   } 
}

then use:

$db = new Database();

Upvotes: 1

Related Questions