Guest User

Untitled

a guest
Oct 17th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. <?php
  2.  
  3. class DatabaseSingleton
  4. {
  5.  
  6. private $_connection;
  7. private static $_instance;
  8. const HOST = "localhost";
  9. const DBNAME = "gsm";
  10. const USER = "root";
  11. const PASS = "root";
  12.  
  13. /*
  14. Get an instance of the Database
  15. @return Instance
  16. */
  17. public static function getInstance()
  18. {
  19. if (!self::$_instance) {
  20. self::$_instance = new self();
  21. }
  22.  
  23. return self::$_instance;
  24. }
  25.  
  26. // Constructor
  27. private function __construct()
  28. {
  29. // $settingsFilePath = __DIR__ . '/.env';
  30. // $settings = parse_ini_file($settingsFilePath);
  31.  
  32. try {
  33. // $dbh = new \PDO(
  34. // "mysql:host={$settings['host']};dbname={$settings['dbName']}", $settings['user'],
  35. // $settings['password']
  36. // );
  37. $dbh = new \PDO(
  38. "mysql:host=" . self::HOST . ";dbname=" . self::DBNAME, self::USER, self::PASS);
  39. $dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
  40.  
  41. $this->_connection = $dbh;
  42.  
  43. } catch (PDOException $e) {
  44. die($e->getMessage());
  45. }
  46. }
  47.  
  48. public function getConnection()
  49. {
  50. return $this->_connection;
  51. }
  52.  
  53. // Magic method clone is empty to prevent duplication of connection
  54. private function __clone()
  55. {
  56. }
  57.  
  58. }
Add Comment
Please, Sign In to add comment