DatabasePDO.class.php 759 Bytes
Newer Older
Zohten's avatar
Zohten committed
1 2
<?php

Zohten's avatar
Zohten committed
3 4 5
class DatabasePDO extends PDO
{
    protected static $singleton = null;
Zohten's avatar
Zohten committed
6

Zohten's avatar
Zohten committed
7 8 9 10 11
    public static function singleton()
    {
        if (is_null(static::$singleton)) {
            static::$singleton = new static();
        }
Zohten's avatar
Zohten committed
12

Zohten's avatar
Zohten committed
13 14
        return static::$singleton;
    }
Zohten's avatar
Zohten committed
15

Zohten's avatar
Zohten committed
16 17 18
    public function __construct()
    {
        // $db = new PDO("sqlite::memory");
Zohten's avatar
Zohten committed
19

Zohten's avatar
Zohten committed
20
        $connectionString = "mysql:host=". DB_HOST;
Zohten's avatar
Zohten committed
21

Zohten's avatar
Zohten committed
22 23 24
        if (defined('DB_PORT')) {
            $connectionString .= ";port=". DB_PORT;
        }
Zohten's avatar
Zohten committed
25

Zohten's avatar
Zohten committed
26 27
        $connectionString .= ";dbname=" . DB_DATABASE;
        $connectionString .= ";charset=utf8";
Zohten's avatar
Zohten committed
28

Zohten's avatar
Zohten committed
29 30 31 32
        parent::__construct($connectionString, DB_USERNAME, DB_PASSWORD);
        $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
}