doudi8519 2016-12-24 16:59 采纳率: 100%
浏览 36
已采纳

如何从扩展类关闭连接

I have a singleton database connection class- db.php (found by googling):

<?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();
    }
}
?>

To test the connectivity, If I extend that database class in ext.php like this:

<?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();
}
}
?>

and in my index.php page:

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

now my output is as below:

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

my question is how can I close connection in extended class (ext.php)?

  • 写回答

1条回答 默认 最新

  • douyu1656 2016-12-24 17:12
    关注

    The solution would be like this:

    • First, create a private instance variable of parent class db in ext class, like this:

      class ext extends db {
          ...
          private $parentInstance;
          ...
      }
      
    • Then use this instance variable $parentInstance to create and close your connection, like this:

      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();
          }
      }
      

    Sidenote: There's one small syntax error in db class as well. The closing parentheses of constructor method is missing.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解