Advertisement
Strider64

Database Class

Nov 15th, 2017
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.38 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Library\Database;
  4.  
  5. # PDO database: only one connection is allowed.
  6.  
  7. use PDO;
  8.  
  9. class Database {
  10.  
  11.     private $_connection;
  12.     // Store the single instance.
  13.     private static $_instance;
  14.  
  15.     // Get an instance of the Database.
  16.     // @return Database:
  17.     public static function getInstance() {
  18.         if (!self::$_instance) {
  19.             self::$_instance = new self();
  20.         }
  21.         return self::$_instance;
  22.     }
  23.  
  24.     // Constructor - Build the PDO Connection:
  25.     public function __construct() {
  26.         $db_options = array(
  27.             /* important! use actual prepared statements (default: emulate prepared statements) */
  28.             PDO::ATTR_EMULATE_PREPARES => false
  29.             /* throw exceptions on errors (default: stay silent) */
  30.             , PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
  31.             /* fetch associative arrays (default: mixed arrays)    */
  32.             , PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
  33.         );
  34.         $this->_connection = new PDO('mysql:host=' . DATABASE_HOST . ';dbname=' . DATABASE_NAME . ';charset=utf8', DATABASE_USERNAME, DATABASE_PASSWORD, $db_options);
  35.     }
  36.  
  37.     // Empty clone magic method to prevent duplication:
  38.     private function __clone() {
  39.        
  40.     }
  41.  
  42.     // Get the PDO connection:    
  43.     public function getConnection() {
  44.         return $this->_connection;
  45.     }
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement