【问题标题】:How can I close connection from extended class如何关闭扩展类的连接
【发布时间】:2016-12-24 16:59:21
【问题描述】:

我有一个单例数据库连接类-db.php(通过谷歌搜索找到):

<?php
/*
* Mysql database class - only one connection alowed
*/
class db {
    private $_connection;
    private static $_instance; //The single instance
    private $_host = "localhost";
    private $_username = "user_name";
    private $_password = "password";
    private $_database = "database";

    /*
    Get an instance of the Database
    @return Instance
    */
    public static function getInstance() {
        if(!self::$_instance) { // If no instance then make one
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    // Constructor
    private function __construct() {
        $this->_connection = new mysqli($this->_host, $this->_username,
            $this->_password, $this->_database);

        // Error handling
        if(mysqli_connect_error()) {
            trigger_error("Failed to conencto to MySQL: " . mysql_connect_error(),
                 E_USER_ERROR);
        }

    // Magic method clone is empty to prevent duplication of connection
    private function __clone() { }

    // Get mysqli connection
    public function getConnection() {
        return $this->_connection;
    }

    public function closeConnection(){
      $this->_connection->close();
    }
}
?>

为了测试连通性,如果我像这样在 ext.php 中扩展该数据库类:

<?php
class ext extends db {
private $conn;
function __construct(){
 $this->connect();
 if(isset($this->conn)){
       echo 'Connection is established<br />';
  }
 $this->closeConn();
}
public function connect(){ 
    $this->conn = parent::getInstance()->getConnection();
}
public function closeConn(){
     parent::closeConnection();
}
}
?>

在我的 index.php 页面中:

<?php
spl_autoload_register(function ($class) {
    include '../classes/' . $class . '.php';
});
$test = new ext();
?>

现在我的输出如下:

Connection is established 
Fatal error: Call to a member function close() on a non-object in (line number) of db.php

我的问题是如何关闭扩展类 (ext.php) 中的连接?

【问题讨论】:

    标签: php database oop mysqli


    【解决方案1】:

    解决方案是这样的:

    • 首先,在ext类中创建父类dbprivate实例变量,如下所示:

      class ext extends db {
          ...
          private $parentInstance;
          ...
      }
      
    • 然后使用此实例变量$parentInstance 来创建和关闭您的连接,如下所示:

      class ext extends db {
          private $conn;
          private $parentInstance;
          function __construct(){
              $this->connect();
              if(isset($this->conn)){
                  echo 'Connection is established<br />';
              }
              $this->closeConn();
          }
          public function connect(){ 
              $this->parentInstance = parent::getInstance();
              $this->conn = $this->parentInstance->getConnection();
          }
          public function closeConn(){
               $this->parentInstance->closeConnection();
          }
      }
      

    旁注db 类中也有一个小的语法错误。构造方法的右括号丢失。

    【讨论】:

    • 关于您的回答,提出了一个相关问题,如果我从另一个类扩展 db 类(例如,将有两个类将扩展 db 类),会有两个连接还是只有一个?
    • @AbdullahMamun-Ur-Rashid 您已将db 类实现为单例模式,因此该类只有一个实例将在它们之间共享另外两个班级。所以那里没有问题。一个小提示是,除了在ext 类的connect() 方法中创建和分配父实例之外,您也可以在构造函数方法中执行此操作。没什么区别。
    • 非常感谢
    猜你喜欢
    • 2019-03-19
    • 2016-05-20
    • 2017-01-20
    • 2011-01-16
    • 1970-01-01
    • 1970-01-01
    • 2012-06-09
    • 2022-06-20
    • 2012-07-28
    相关资源
    最近更新 更多