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 83 84 85 86 87 88 89 90
   |  
class Database {
 
    protected static $_pdo_instance;
 
    private function __construct () { }
 
    public function instance () {
        if (!isset(static::$_pdo_instance)) {
            $args = func_get_args();
            try {
                switch (count($args)) {
                    case 0:
                        throw new InvalidArgumentException("The first parameter is mandatory"); break;
                    case 1:
                        static::$_pdo_instance = new PDO($args[0]);
                        break;
                    case 2:
                        static::$_pdo_instance = new PDO($args[0], $args[1]);
                        break;
                    case 3:
                        static::$_pdo_instance = new PDO($args[0], $args[1], $args[2]);
                        break;
                    case 4:
                        static::$_pdo_instance = new PDO($args[0], $args[1], $args[2], $args[3]);
                        break;
                    default:
                        throw new BadMethodCallException(__METHOD__ . " takes at least 1 parameter and at most 4 parameters");
                        break;
                }
            }
            catch (Exception $e) {
                throw new RuntimeException("Cannot connect to database", 0, $e);
            }
        }
        return static::$_pdo_instance;
    }
 
    public static function beginTransaction () {
        return static::$_pdo_instance->beginTransaction();
    }
 
    public static function commit () {
        return static::$_pdo_instance->commit();
    }
 
    public static function errorCode () {
        return static::$_pdo_instance->errorCode();
    }
 
    public static function errorInfo () {
        return static::$_pdo_instance->errorInfo();
    }
 
    public static function exec ($statement) {
        return static::$_pdo_instance->exec($statement);
    }
 
    public static function getAttribute ($attribute) {
        return static::$_pdo_instance->getAttribute($attribute);
    }
 
    public static function getAvailableDrivers () {
        return static::$_pdo_instance->getAvailableDrivers();
    }
 
    public static function lastInsertId ($name = null) {
        return static::$_pdo_instance->lastInsertId($name);
    }
 
    public static function prepare ($statement, $driver_options = array()) {
        return static::$_pdo_instance->prepare($statement, $driver_options);
    }
 
    public static function query ($statment) {
        return static::$_pdo_instance->query($statment);
    }
 
    public static function quote ($string, $parameter_type = PDO::PARAM_STR) {
        return static::$_pdo_instance->quote($string, $parameter_type);
    }
 
    public static function rollBack () {
        return static::$_pdo_instance->rollBack();
    }
 
    public static function setAttribute ($attribute, $value) {
        return static::$_pdo_instance->setAttribute($attribute, $value);
    }
} | 
Partager