interface DatabaseInteface
{
public function Connection();
}
/* Open a Connection to PDO */
class PDOConnectionDatabase implements DatabaseInteface {
/* Properties */
private $conn;
private $dsn = 'mysql:dbname=panel;host=localhost';
private $user = 'root';
private $password = '';
public function Connection()
{
try {
$this->conn = new PDO($this->dsn, $this->user, $this->password);
// set the PDO error mode to exception
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "PDO Connected successfully";
} catch(PDOException $e) {
echo "PDO Connection failed: " . $e->getMessage();
}
}
}
/* Open a Connection to MySQL */
class mysqlConnectionDatabase implements DatabaseInteface {
/* Properties */
private $conn;
private $serve = 'localhost';
private $user = 'root';
private $password = '';
public function Connection()
{
$this->conn = mysqli_connect($this->serve, $this->user, $this->password);
// Check connection
if (!$this->conn) {
die("mysqli Connection failed: " . mysqli_connect_error());
}
echo "mysqli Connected successfully";
}
}
class Users
{
protected DatabaseInteface $DatabaseInteface;
public function __construct(DatabaseInteface $DatabaseInteface)
{
$this->DatabaseInteface = $DatabaseInteface;
}
public function UserConectToDatabase()
{
$this->DatabaseInteface->Connection();
}
}
/* MySQLi Installation */
$mysqlconnection = new mysqlConnectionDatabase();
/* PDO Installation */
$PDOconnection = new PDOConnectionDatabase();
$newUser = new Users($PDOconnection);
$newUser->UserConectToDatabase();