繁体   English   中英

类,实例和对象的问题

[英]Issue with Classes, Instances and Objects

我刚接触PHP; 但是,我想我对此很有把握,但是在理解为什么我无法访问我拥有的连接到数据库类的第二版本时遇到了一个问题。

我在配置脚本中全局连接到第一个。

$db = new db(array('login info'));

我使用以下命令在控制器中访问它。

public $db;
public function __construct(){
    $this->db = $GLOBALS['db'];
}

然后在我的控制器内部,我有一个函数,可以在其中创建一个新实例以连接到另一个数据库。

$ordersDB = new db(array('login info'));

现在我想使用类似的方法分别访问每个数据库

$this->db->select("SELECT * FROM ada Yada 

$ordersDB->select("SELECT * FROM Yada Yada

但是,两者仍在访问第一个数据库。 我知道这一点,因为执行第二个表时我得到的表不存在错误,它告诉我对其查询的数据库。 但是,其中的var_export表明它们实际上是不同的! 我是否完全误解了类的工作方式,还是一次只能打开一个数据库? 谢谢你的帮助。

编辑:这是您在寻找什么?

$db = new db(array(connect info));
$controller = new controller();
$controller->connectToSecondDB();

class db {

    public $dbHost;
    public $dbUser;
    public $dbName;
    public $dbPassword;

    public function __construct() {
       $arguments = func_get_args();

       if(!empty($arguments)){
          foreach($arguments[0] as $key => $property){
            if(property_exists($this, $key)){
                $this->{$key} = $property;
            }
           }
       }
    }

  public function connect() {            
        if(!isset(self::$connection)) {       
            self::$connection = new mysqli($this->dbHost, $this->dbUser, $this->dbPassword, $this->dbName);
        }
}

class controller {
   public $db;
   public function __construct(){
      $this->db = $GLOBALS['db'];
   }

   public function connectToSecondDB(){
      $ordersDB = new db(array(connect info));
      $ordersDB->select("SELECT * FROM `customer` WHERE email = 'email@address.com' LIMIT 1")
      $this->db->query("SQL");
   }
}

编辑选择查询功能

private function _sel($table, $where="", $order_by="", $limit="", $group_by="",$database = NULL){
        if($database === NULL) {  $database = $this->db; }
        if($limit == 1){ $single = true; }else{ $single = false; }

        //if( is_array($where) ){ $this->_buildWhere(); }
        $where = (strlen($where) > 0) ? "WHERE $where " : $where;       
        $group_by = (strlen($group_by) > 0) ? "GROUP BY $group_by " : $group_by;        
        $order_by = (strlen($order_by) > 0) ? "ORDER BY $order_by " : $order_by;        
        $limit = (strlen($limit) > 0) ? "LIMIT $limit " : $limit ;

        $sql = "SELECT * 
                FROM `$table` 
                $where 
                $group_by 
                $order_by 
                $limit";
        // Debug

//if(INCLUDE_CHECK){ echo "<HR>".$sql."<HR>";   }
        $results = $database->select($sql); 
        if($single && count($results) > 0 ){
            return($results[0]);
        }else{
            return($results);
        }
    }

一个解决方案是重用$ controller对象。 添加query方法。 然后,您可以与连接方法分开运行查询,以便可以重用控制器对象

您需要在执行过程中对每个步骤进行错误检查。

请注意,我删除了$this->db->query("SQL"); 因为我不确定它到底做了什么。 答案中的概念应使您开始重新设计课程。

更新基于注释,我从超级全局更改了$ db并将其传递给控制器​​对象。

$db = new db(array(connect info));
// try passing the $db object so it does not need to be a super global
$controller = new controller($db);
// You can keep your $controller object as a global - I use a session variable for this to reuse it between pages.
$controller->connectToSecondDB();
// run the query separate from the connection so the controller object can be reused
$result = $controller->ordersQuery("SELECT * FROM `customer` WHERE email = 'email@address.com' LIMIT 1")


class db {

    public $dbHost;
    public $dbUser;
    public $dbName;
    public $dbPassword;
    public $connection;

    public function __construct() {
       $arguments = func_get_args();

       if(!empty($arguments)){
          foreach($arguments[0] as $key => $property){
            if(property_exists($this, $key)){
                $this->{$key} = $property;
            }
           }
       }
    }

  public function connect() {            
        if(!isset($this->$connection)) {       
            $this->connection = new mysqli($this->dbHost, $this->dbUser, $this->dbPassword, $this->dbName);
        }
}

class controller() {
   public $db;
   public $ordersDB;    // ADD THIS

   public function __construct($db2){
      $this->db = $db2;
   }

   public function connectToSecondDB(connect info){
      $ordersDB = new db(array(connect info));
   }

   public function ordersQuery($sql){
      $this->ordersDB->query($sql)
      // Fetch and return you result set here
   }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM