Advertisement
Guest User

Untitled

a guest
May 23rd, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Singleton Class Database used to connect to the database while only making one connection
  5. * @author Wesley Klop<wesley19097@gmail.com>
  6. */
  7. final class Database
  8. {
  9. /** @var PDO $dbh */
  10. private static $dbh;
  11. /** @var string $username */
  12. private static $username = '';
  13. /** @var string $password */
  14. private static $password = '';
  15. /** @var string $dsn */
  16. private static $dsn = "mysql:host=localhost;dbname=" . self::$username . ";charset=UTF8";
  17. /** @var array $options */
  18. private static $options = array(
  19. PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'",
  20. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  21. );
  22.  
  23. /** @ignore */
  24. public function __construct()
  25. {
  26. }
  27.  
  28. /** @ignore */
  29. public function __clone()
  30. {
  31. }
  32.  
  33. /**
  34. * @return PDO $dbh database handle
  35. * @throws PDOException
  36. */
  37. public static function getInstance()
  38. {
  39. if (!(self::$dbh instanceof PDO)) {
  40. self::$dbh = new PDO(self::$dsn, self::$username, self::$password, self::$options);
  41. }
  42. return self::$dbh;
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement