我想在getInstance方法中更改参数时获取新的连接对象 $ data1 = DB :: getInsatance(' common') - >查询("从表1中选择*(共同)"); $ data2 = DB :: getInsatance(' misc') - >查询("从表2中选择*(在misc中)");
但是,无论何时使用单独的用户创建两个实例方法,都不会生成稍后在流中启动的对象
<!--
This is my DB class where my pdo parameters will change based on arguement supplied in getInstance() method --->
<?php
include_once 'Config.php';
class DB {
private static $_instance = null;
private $_pdo, $_query, $_error = false, $_results, $_count = 0;
protected $user;
private function __construct($user) {
try {
$this -> _pdo = new PDO('oci:dbname=//' . Config::get($user.'/host') . '/' . Config::get($user.'/db'), Config::get($user.'/username'), Config::get($user.'/password'));
} catch (PDOException $e) {
die($e -> getMessage());
}
}
public static function getInstance($user) {
if (!isset(self::$_instance)) {
self::$_instance = new DB($user);
}
//return $user;
return self::$_instance;
}
public function query($sql, $params = array()) {
//echo $sql.'<br>';
$this -> _error = false;
if ($this -> _query = $this -> _pdo -> prepare($sql)) {
$x = 1;
if (count($params)) {
foreach ($params as $param) {
$this -> _query -> bindValue($x, $param);
$x++;
}
}
if ($this -> _query -> execute()) {
$this -> _results = $this -> _query -> fetchAll(PDO::FETCH_OBJ);
foreach ($this ->_results as $result) {
$this -> _count++;
}
//return $this ->_count;
} else {
$this -> _error = true;
}
}
return $this;
}
public function action($action, $table, $where = array()) {
if (count($where) === 3) {
$operators = array('=', '>', '<', '>=', '<=','like');
$field = $where[0];
$operator = $where[1];
$value = $where[2];
if (in_array($operator, $operators)) {
$sql = "{$action} FROM {$table} WHERE {$field} {$operator} ?";
if (!$this -> query($sql, array($value)) -> error()) {
return $this;
}
}
}
return false;
}
public function get($table, $where) {
return $this -> action('SELECT *', $table, $where);
}
public function delete($table, $where) {
return $this -> action('DELETE', $table, $where);
}
public function getf($keys = array(), $table, $wheres = array(array(),array(),array())) {
$fields = $wheres[0];
$qoperator = $wheres[1];
$values = $wheres[2];
$operators = array('=', '>', '<', '>=', '<=');
foreach ($fields as $field) {
$this -> field = $field;
}
foreach ($qoperator as $operator1) {
$this -> operator = $operator1;
}
foreach ($values as $value1) {
$this -> value = $value1;
}
//if (in_array($operator, $operators)) {
if (count($fields) === count($values)) {
$sql = "SELECT " . implode(',', $keys) . "FROM {$table} WHERE" . implode('AND', '{$field} {$operator} ?');
} elseif (count($fields) === 0) {
$sql = "SELECT " . implode(',', $keys) . "FROM {$table} WHERE" . implode('AND', '{$field} {$operator} ?');
}
if (!$this -> query($sql, array($value)) -> error()) {
return $this;
}
//}
return false;
}
public function select($keys, $table, $where) {
return $this -> getf($keys, $table, $where);
}
public function insert($table, $fields = array()) {
$keys = array_keys($fields);
$values = '';
$x = 1;
foreach ($fields as $field) {
$values .= '?';
if ($x < count($fields)) {
$values .= ', ';
}
$x++;
}
$sql = "INSERT INTO {$table} (" . implode(',', $keys) . ") VALUES ({$values})";
if (!$this -> query($sql, $fields) -> error()) {
return true;
}
return false;
}
public function update($table, $id, $fields) {
$set = '';
$x = 1;
foreach ($fields as $name => $value) {
$set .= "{$name} = ?";
if ($x < count($fields)) {
$set .= ', ';
}
$x++;
}
$sql = "UPDATE {$table} SET {$set} WHERE TENDER_ID = {$id}";
if (!$this -> query($sql, $fields) -> error()) {
return true;
}
return false;
}
public function results() {
return $this -> _results;
}
public function first() {
//return $this->results()[0];
return current($this -> results());
}
public function error() {
return $this -> _error;
}
public function count() {
return $this -> _count;
}
}
?>
&#13;
答案 0 :(得分:0)
不是在用户更改时设置$ user并添加新连接,而是可以存储连接并仅在不存在时建立新连接:
private function __construct($user) {
try {
$this->_pdo = new PDO(
'oci:dbname=//'.Config::get($user.'/host').'/'.Config::get($user.'/db'),
Config::get($user.'/username'),
Config::get($user.'/password')
);
} catch (PDOException $e) {
die($e -> getMessage());
}
}
public static function getInstance($user) {
if (!empty(self::$_instance[$user])) {
self::$_instance[$user] = new DB($user);
}
return self::$_instance[$user];
}