DBv1

 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
<?php namespace Redozote\Database;
class DB extends \PDO {
private static $_instance;
private static $_statement;
public function __construct() {
try {
parent::__construct('mysql:host='.$db_host.';dbname='.$db_name, $db_user, $db_pass);
$this->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
$this->query('SET NAMES `utf8` COLLATE `utf8_unicode_ci`');
} catch (PDOException $e) {
}
}
private function __clone() {}
public static function __callStatic($method, $arguments) {
$instance = self::getInstance();
call_user_func_array(array($instance, $method), $arguments);
}
public static function getInstance() {
if (!self::$_instance) self::$_instance = new self();
return self::$_instance;
}
public function query($query, $params = array()) {
$query = self::$_instance->bindParams($query, $params);
self::$_statement = parent::query($query);
return $this;
}
public function bindParams($query, $params = array()) {
$count = count($params);
for ($i = 0; $i < $count; ++$i) {
$pos = strpos($query, '?');
$param = parent::quote($params[$i]);
$query = substr_replace($query, $param, $pos, 1);
}
return $query;
}
}