I read many posts and blogs about Singleton and database connection. What I understand from the singleton definition is that you can use it on any class object that is instantiated many times while staying intact. I have a web application, and I was wondering if I could implement Singleton for database connection
Here is what I have under the DatabaseGateway class:
class DatabaseGateway
{
private $DBConnection;
private $serverName;
private $userName;
private $password;
private $databaseName;
public function __construct() {
//not done
}
public function getDBConnection() {
return $this->DBConnection;
}
public function openDBConnection() {
$this->DBConnection = new mysqli($this->serverName, $this->userName, $this->password, $this->databaseName);
}
public function closeDBConnection() {
mysqli_close($this->DBConnection);
}
The method OpenDBConnection is used when users log in in the UserGateway class. I was wondering if it is a good idea, and if it possible, to make openDBconnection into a singleton, while everyone has their own account and password.