-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.db.php
More file actions
83 lines (72 loc) · 1.71 KB
/
Copy pathclass.db.php
File metadata and controls
83 lines (72 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
namespace Module\DB;
/**
* Implements a singleton database class
*
* This abstract class is to ensure that only one PDO instance is used. You get
* a instance of the database with:
*
* ### Usage
*
* <code>
* \Module\DB\DB::$dbhost = 'localhost';
* \Module\DB\DB::$dbname = 'db';
* \Module\DB\DB::$dbuser = 'username';
* \Module\DB\DB::$dbpass = 'itsmeopenup';
* $db = \Module\DB\DB::getInstance();
* </code>
*
* ### Changelog
*
* ## Version 1.12
* * Added the date section to the documentation
*
* @date August 13, 2014
* @author Jaime A. Rodriguez <hi.i.am.jaime@gmail.com>
* @version 1.2
* @license http://opensource.org/licenses/MIT
*/
abstract class DB {
/**
* PDO The single instance is stored here.
* @private
*/
private static $instance = NULL;
/**
* string The database host
*/
public static $dbhost = DBHOST;
/**
* string The database name
*/
public static $dbname = DBNAME;
/**
* string The database user
*/
public static $dbuser = DBUSER;
/**
* string The database password
*/
public static $dbpass = DBPASS;
/**
* Sets constructor to private, prevents new instances
*/
private function __construct() {}
/**
* Sets __clone to private, prevents clones
*/
private function __clone(){}
/**
* Gets the DB instance or creates an initial connection
*
* @return object (PDO)
*/
public static function getInstance() {
if (!self::$instance) {
self::$instance = new \PDO("mysql:host=" . self::$dbhost . ";dbname=" . self::$dbname, self::$dbuser, self::$dbpass);
self::$instance-> setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
self::$instance-> setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
}
return self::$instance;
}
}