2016-12-24 95 views
0

连接我有一个单独的数据库连接类 - db.php中(通过在google找到):如何关闭从扩展的类

<?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)中的连接?

回答

1

解决办法是这样的:

  • 首先,在ext类创建父db类的private实例变量,像这样:

    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类中的一个小的语法错误也是如此。构造函数方法的右括号缺失。

+0

关于你的回答,提出了一个相关的问题,如果我从其他类扩展db类(例如将有两个类将扩展db类),会有两个连接还是只有一个? –

+0

@ AbdullahMamun-Ur-Rashid您已经将'db'类实现为Singleton模式,因此将只有一个**类的实例**,它将在另外两个类之间共享。所以没有问题。一个小问题是,不是在'ext'类的'connect()'方法中创建和分配父实例,而是在构造方法中也可以这样做。这没有什么区别。 –

+0

非常感谢 –