Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class DatabaseSingleton
- {
- private $_connection;
- private static $_instance;
- const HOST = "localhost";
- const DBNAME = "gsm";
- const USER = "root";
- const PASS = "root";
- /*
- Get an instance of the Database
- @return Instance
- */
- public static function getInstance()
- {
- if (!self::$_instance) {
- self::$_instance = new self();
- }
- return self::$_instance;
- }
- // Constructor
- private function __construct()
- {
- // $settingsFilePath = __DIR__ . '/.env';
- // $settings = parse_ini_file($settingsFilePath);
- try {
- // $dbh = new \PDO(
- // "mysql:host={$settings['host']};dbname={$settings['dbName']}", $settings['user'],
- // $settings['password']
- // );
- $dbh = new \PDO(
- "mysql:host=" . self::HOST . ";dbname=" . self::DBNAME, self::USER, self::PASS);
- $dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
- $this->_connection = $dbh;
- } catch (PDOException $e) {
- die($e->getMessage());
- }
- }
- public function getConnection()
- {
- return $this->_connection;
- }
- // Magic method clone is empty to prevent duplication of connection
- private function __clone()
- {
- }
- }
Add Comment
Please, Sign In to add comment