未捕获错误:在null

时间:2016-05-14 14:57:45

标签: php mysqli

我想通过调用$connection函数关闭close()

class Db {
protected static $connection;

// Connect to the database
public function connect() {

  if(!isset(self::$connection)) {
    $config = parse_ini_file('database.ini');
    self::$connection = new mysqli($config['hostname'], $config['username'], $config['password'], $config['dbname']);

    if(self::$connection == false){
      //Handle error or send to the admin
      return false;
    }
    return self::$connection;
  }
}
public function close(){
  $connection = $this -> connect();
  $connection -> close();
}
}

但我得到的错误是:未捕获的错误:Call to a member function close() on null
是因为static $connection吗?
我可以使用unset($obj)销毁对象而不必担心$connection吗?
关闭连接的正确方法是什么?

0 个答案:

没有答案