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