Instantly share code, notes, and snippets.

Embed
What would you like to do?
An class for a PHP PDO connection - a singleton implementation. An instance method returns a new PDO Connection with the specified connection attributes.
<?php
namespace ACompany\AnAppName\Dao\PdoImpl;
/**
* PDOConnection is a singleton implementation.
* getConnection() returning an instance of PDO connection.
*
* <code>
* Example usage:
*
* $pdo = PDOConnection::instance();
* $conn = $pdo->getConnection( 'dsn', 'username', 'password' );
*
* $results = $conn->query("SELECT * FROM Table");
*
* </code>
*
* @author rmurray
*/
class PDOConnection {
/**
* singleton instance
*
* @var PDOConnection
*/
protected static $_instance = null;
/**
* Returns singleton instance of PDOConnection
*
* @return PDOConnection
*/
public static function instance() {
if ( !isset( self::$_instance ) ) {
self::$_instance = new PDOConnection();
}
return self::$_instance;
}
/**
* Hide constructor, protected so only subclasses and self can use
*/
protected function __construct() {}
function __destruct(){}
/**
* Return a PDO connection using the dsn and credentials provided
*
* @param string $dsn The DSN to the database
* @param string $username Database username
* @param string $password Database password
* @return PDO connection to the database
* @throws PDOException
* @throws Exception
*/
public function getConnection($dsn, $username, $password) {
$conn = null;
try {
$conn = new \PDO($dsn, $username, $password);
//Set common attributes
$conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
return $conn;
} catch (PDOException $e) {
//TODO: flag to disable errors?
throw $e;
}
catch(Exception $e) {
//TODO: flag to disable errors?
throw $e;
}
}
/** PHP seems to need these stubbed to ensure true singleton **/
public function __clone()
{
return false;
}
public function __wakeup()
{
return false;
}
}
@jorygun

This comment has been minimized.

Copy link

jorygun commented Jul 26, 2016

Rob,
I have been struggling with transitioning to OO and PDO. I've decided that your script here is the best
comprehensible example I can use, but I don't understand a couple of things about this script. (Apologize for my ignorance.)

  1. Other singleton examples I find put the new PDO in the __construct. Why do you not use the __construct?
  2. I don't understand what the instance function is. Why isn't the code in instance() in getConnection(), and the code in getConnection in __contruct? I can't seem to find an explanation of instance on the web that I can understand.
  3. I had to change the getConnection to static to remove a warning. (I am defining the connection stuff inside the function instead of passing them. Maybe that's why?)
@jspringe

This comment has been minimized.

Copy link

jspringe commented Jun 20, 2017

@jorygun - You are right to have questions about why he wasn't instantiating PDO in the constructor. Try my fork out and see if it works. It's only loosely tested, and not "production hardened" (nor is this code), but it better represents (IMHO) how this should look.

@odan

This comment has been minimized.

Copy link

odan commented Jul 5, 2017

@amenadiel

This comment has been minimized.

Copy link

amenadiel commented Nov 28, 2018

I believe $conn should be a protected static, so you actually return the same connection everytime. Otherwise, you return the same instance of the class when calling PDOConnection::instance() but you create a new connection when you call PDOConnection::getConnection()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment