1

我在下面有 PDO 类:

class DB {

        private $dbh;
        private $stmt;

        static $db_type;
        static $connections;

        public function __construct($db, $id="") {

             switch($db) {
                case "db1":
                      try{

                          $this->dbh = new PDO("mysql:host=localhost;dbname=ms".$id, 'root', '', array( PDO::ATTR_PERSISTENT => true ));
                      }  catch(PDOException $e){
                          print "Error!: " . $e->getMessage() . "<br />";
                          die();
                      }
                break;
                case "db2":
                      try{
                          $this->dbh = new PDO("mysql:host=localhost;dbname=users", 'root', '', array( PDO::ATTR_PERSISTENT => true ));
                      } catch(PDOException $e){
                          print "Error!: " . $e->getMessage() . "<br />";
                          die();
                      }
                break;
            }
            self::$db_type = $db;
        }

        static function init($db_type = ""){ 

            print_r(self::$connections);


            if(!isset(self::$connections[$db_type])){ 
                self::$connections[$db_type] = new self($db_type); 
            } 

            return self::$connections[$db_type];
        }

        public static function query($query) {

            self::$connections[self::$db_type]->stmt = self::$connections[self::$db_type]->dbh->prepare($query);
            return self::$connections[self::$db_type];
        }

        public function bind($pos, $value, $type = null) {

            if( is_null($type) ) {
                switch( true ) {
                    case is_int($value):
                        $type = PDO::PARAM_INT;
                        break;
                    case is_bool($value):
                        $type = PDO::PARAM_BOOL;
                        break;
                    case is_null($value):
                        $type = PDO::PARAM_NULL;
                        break;
                    default:
                        $type = PDO::PARAM_STR;
                }
            }

            self::$connections[self::$db_type]->stmt->bindValue($pos, $value, $type);
            return self::$connections[self::$db_type];
        }

        public function execute() {
            return self::$connections[self::$db_type]->stmt->execute();
        }
    }

接下来我尝试:

$id = 1;
DB::init('db1', $id);

这给我一个错误:

Error!: SQLSTATE[HY000] [1049] Unknown database 'ms'<br />

为什么我的数据库名称在连接期间是 ms,因为它应该是 ms1?谢谢。

4

1 回答 1

2

您的 init 方法的签名是:

 static function init($db_type = "")

这意味着它只接受一个参数,而你这样称呼它:

DB::init('db1', $id);

这是行不通的。另外:您需要阅读static持久连接和注入与单例...您的代码充满了问题。首先:始终指定访问修饰符。
您的标题表明想要使用多重连接,但您$db_type一遍又一遍地重新分配属性(它是静态的,因此在所有实例中共享)。
您正在尝试使用该Singleton模式,无论如何这在 PHP 中毫无意义,但在您的情况下更是如此,因为您的构造函数是公共的,仍然......

仅在必要时才使用静态,即便如此:仔细考虑:大多数时候,必须使用静态意味着必须承认设计错误。

query方法只接受一个参数:一个字符串,并在最后建立的连接上执行该查询。您无法选择此查询将在哪个数据库上运行。如果不危险的话,那就是这样,我不想成为一个混蛋,但我不能说其他任何方式:这是可怕的代码

请重构这段代码,如果我发现自己不得不使用这个类,我会创建自己的实例PDO并使用它。不管你怎么看:你大大限制了一个人可以执行的查询,但你不能拒绝我对PDO自己的访问......

于 2013-09-02T11:52:55.107 回答