Skip to content

Instantly share code, notes, and snippets.

@SergeyMiracle
Forked from rob-murray/PDOConnection.php
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SergeyMiracle/d342c146463e9e944870 to your computer and use it in GitHub Desktop.
Save SergeyMiracle/d342c146463e9e944870 to your computer and use it in GitHub Desktop.
<?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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment