Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.85 KB | None | 0 0
  1. <?php
  2. namespace Core;
  3. use \PDO;
  4.  
  5. class Database
  6. {
  7.     private $instance = null;
  8.  
  9.     private $username;
  10.     private $password;
  11.     private $server;
  12.     private $dbname;
  13.  
  14.     public function __construct($username, $password, $server, $dbname)
  15.     {
  16.         $this->username = $username;
  17.         $this->password = $password;
  18.         $this->server = $server;
  19.         $this->dbname = $dbname;
  20.     }
  21.  
  22.     public function getInstance()
  23.     {
  24.         if ($this->instance == null) {
  25.             $this->createInstance();
  26.         }
  27.  
  28.         return $this->instance;
  29.     }
  30.  
  31.     private function createInstance() {
  32.         $this->instance = new PDO("mysql:host={$this->server};dbname={$this->dbname};charset=utf8", $this->username, $this->password);
  33.         $this->instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement