Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace ACompany\AnAppName\Dao\PdoImpl;
- use PDO;
- use Exception;
- use PDOException;
- /**
- * UPDATE: I'm leaving the author's original comments, but this has been updated to better represent a singleton PDO
- * connection. The author's original code would create a new connection on each call to getConnection.
- *
- * The other thing it did was set all of these methods and properties to 'protected', but then called 'self' - if you
- * want this class to be extendable then you should use 'static' due to PHP's 'Late static binding'.
- *
- * Also I'm using the PSR-2 style guidelines.
- *
- * While I'm NOT condoning this as a 'best practice' I thought I would at least correct the misconception that this was
- * creating a single connection.
- *
- * Usage now is:
- * $pdo = PDOConnection::instance('dsn', 'username', 'password'); // first call only.
- * $connection = $pdo->getConnection();
- *
- * Once you know the instance has been created you can just call: $pdo = PDOConnection::instance();
- *
- **** ORIGINAL COMMENTS ****
- *
- * 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
- {
- protected static $instance = null;
- /** @var PDO */
- protected $connection = null;
- /**
- * PDOConnection constructor.
- * @param $dsn
- * @param $username
- * @param $password
- * @throws PDOException
- * @throws Exception
- */
- protected function __construct($dsn, $username, $password)
- {
- try {
- $this->connection = new PDO($dsn, $username, $password);
- //Set common attributes
- $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- } catch (PDOException $e) {
- //TODO: flag to disable errors?
- throw $e;
- } catch(Exception $e) {
- //TODO: flag to disable errors?
- throw $e;
- }
- }
- /**
- * Get the current active instance if one exists, otherwise create a new instance.
- *
- * @param string|null $dsn
- * @param string|null $username
- * @param string|null $password
- * @throws PDOException
- * @throws Exception
- * @return PDOConnection
- */
- public static function instance($dsn = null, $username = null, $password = null) {
- if (!isset(static::$instance)) {
- if (!isset($dsn) || !isset($username) || !isset($password)) {
- throw new Exception('No instance exists. You must provide the dsn, username, and password.');
- }
- static::$instance = new static($dsn, $username, $password);
- }
- return static::$instance;
- }
- /**
- * Return a PDO connection
- *
- * @return PDO
- */
- public function getConnection()
- {
- return $this->connection;
- }
- /** PHP seems to need these stubbed to ensure true singleton **/
- private function __clone() {}
- private function __wakeup() {}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement