我想通过调用$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
吗?
关闭连接的正确方法是什么?