-
Notifications
You must be signed in to change notification settings - Fork 0
/
Db.php
77 lines (59 loc) · 1.48 KB
/
Db.php
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
<?php
/*
* @author : Andy Fitria
* simple pdo class
* https://sintret.com
*/
class Db {
/**
* singleton instance
*
* @var PDOConnection
*/
protected static $_instance = null;
/**
* Returns singleton instance
*
*/
public static function instance()
{
if (!isset(self::$_instance)) {
$conn = null;
try {
include dirname(__FILE__) . '\config.php';
$conn = new \PDO("mysql:host=$host;dbname=$db", $username, $password);
//Set common attributes
$conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
return $conn;
} catch (PDOException $e) {
//TODO: flag to disable errors?
echo $e->getMessage() . $e->getLine();
throw $e;
} catch (Exception $e) {
//TODO: flag to disable errors?
echo $e->getMessage() . $e->getLine();
throw $e;
}
}
return self::$_instance;
}
/**
* Hide constructor, protected so only subclasses and self can use
*/
protected function __construct()
{
}
function __destruct()
{
}
/** PHP seems to need these stubbed to ensure true singleton * */
public function __clone()
{
return false;
}
public function __wakeup()
{
return false;
}
}