Advertisement
Guest User

Untitled

a guest
Jun 20th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. <?php
  2.  
  3. namespace ACompany\AnAppName\Dao\PdoImpl;
  4.  
  5. use PDO;
  6. use Exception;
  7. use PDOException;
  8. /**
  9. * UPDATE: I'm leaving the author's original comments, but this has been updated to better represent a singleton PDO
  10. * connection. The author's original code would create a new connection on each call to getConnection.
  11. *
  12. * The other thing it did was set all of these methods and properties to 'protected', but then called 'self' - if you
  13. * want this class to be extendable then you should use 'static' due to PHP's 'Late static binding'.
  14. *
  15. * Also I'm using the PSR-2 style guidelines.
  16. *
  17. * While I'm NOT condoning this as a 'best practice' I thought I would at least correct the misconception that this was
  18. * creating a single connection.
  19. *
  20. * Usage now is:
  21. * $pdo = PDOConnection::instance('dsn', 'username', 'password'); // first call only.
  22. * $connection = $pdo->getConnection();
  23. *
  24. * Once you know the instance has been created you can just call: $pdo = PDOConnection::instance();
  25. *
  26. **** ORIGINAL COMMENTS ****
  27. *
  28. * PDOConnection is a singleton implementation.
  29. * getConnection() returning an instance of PDO connection.
  30. *
  31. * <code>
  32. * Example usage:
  33. *
  34. * $pdo = PDOConnection::instance();
  35. * $conn = $pdo->getConnection( 'dsn', 'username', 'password' );
  36. *
  37. * $results = $conn->query("SELECT * FROM Table");
  38. *
  39. * </code>
  40. *
  41. * @author rmurray
  42. */
  43. class PDOConnection
  44. {
  45. protected static $instance = null;
  46.  
  47. /** @var PDO */
  48. protected $connection = null;
  49. /**
  50. * PDOConnection constructor.
  51. * @param $dsn
  52. * @param $username
  53. * @param $password
  54. * @throws PDOException
  55. * @throws Exception
  56. */
  57. protected function __construct($dsn, $username, $password)
  58. {
  59. try {
  60. $this->connection = new PDO($dsn, $username, $password);
  61. //Set common attributes
  62. $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  63. } catch (PDOException $e) {
  64. //TODO: flag to disable errors?
  65. throw $e;
  66. } catch(Exception $e) {
  67. //TODO: flag to disable errors?
  68. throw $e;
  69. }
  70. }
  71.  
  72. /**
  73. * Get the current active instance if one exists, otherwise create a new instance.
  74. *
  75. * @param string|null $dsn
  76. * @param string|null $username
  77. * @param string|null $password
  78. * @throws PDOException
  79. * @throws Exception
  80. * @return PDOConnection
  81. */
  82. public static function instance($dsn = null, $username = null, $password = null) {
  83. if (!isset(static::$instance)) {
  84. if (!isset($dsn) || !isset($username) || !isset($password)) {
  85. throw new Exception('No instance exists. You must provide the dsn, username, and password.');
  86. }
  87.  
  88. static::$instance = new static($dsn, $username, $password);
  89. }
  90.  
  91. return static::$instance;
  92. }
  93.  
  94. /**
  95. * Return a PDO connection
  96. *
  97. * @return PDO
  98. */
  99. public function getConnection()
  100. {
  101. return $this->connection;
  102. }
  103. /** PHP seems to need these stubbed to ensure true singleton **/
  104. private function __clone() {}
  105. private function __wakeup() {}
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement