# OOP and connecting to multiple databases using PDO

I'm just starting to learn OOP and I've setup a class to handle the database connections and wanted to check if I'm going about this the right way.

My website is already using PDO for prepared statements but it's procedural code otherwise. The site connects to multiple databases and one of the databases has two connections for separate privileges.

I have created a Dsn class which contains a method for each connection. My config file instantiates the class and opens all the connections using the methods and passing params where necessary. I'll put the code below but just wanted to check if connecting to multiple databases in this way through a single class follows best practices for OOP.

config.php

$dbh = new Dsn;$pdo = $dbh->connnect1Read(NAME1, USER2, PASS2);$pdo2 = $dbh->connnect2Write(NAME1, USER1, PASS1);$pdo3 = $dbh->connnect3(USER2, PASS2);$pdo4 = $dbh->connnect4(USER3, PASS3);$pdo5 = $dbh->connnect5(USER4, PASS4);  dsn.php  class Dsn { private$host = 'localhost';
private $charset = 'utf8mb4'; private$dbName1;
private $userName1; private$userPass1;

private $dbName2 = 'db2'; private$userName2;
private $userPass2; private$dbName3 = 'db3';
private $userName3; private$userPass3;

private $dbName4 = 'db4'; private$userName4;
private $userPass4; private$opt = [
PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES   => false,
];

public function connnect1Read($dbName,$userName, $userPass) {$this->dbName1 = $dbName;$this->userName2 = $userName;$this->userPass2 = $userPass;$dsn = "mysql:host=" . $this->host . ";dbname=" .$this->dbName1 . ";charset=" . $this->charset . "";$pdo1 = new PDO($dsn,$this->userName2, $this->userPass2,$this->opt);

return $pdo1; } public function connnect2Write($dbName1, $userName,$userPass)
{
$this->dbName1 =$dbName1;
$this->userName1 =$userName;
$this->userPass1 =$userPass;

$dsn = "mysql:host=" .$this->host . ";dbname=" . $this->dbName1 . ";charset=" .$this->charset . "";
$pdo2 = new PDO($dsn, $this->userName1,$this->userPass1, $this->opt); return$pdo2;
}

public function connnect3($userName,$userPass)
{
$this->userName2 =$userName;
$this->userPass2 =$userPass;

$dsn = "mysql:host=" .$this->host . ";dbname=" . $this->dbName2 . ";charset=" .$this->charset . "";
$pdo4 = new PDO($dsn, $this->userName2,$this->userPass2, $this->opt); return$pdo3;
}

public function connnect4($userName,$userPass)
{
$this->userName3 =$userName;
$this->userPass3 =$userPass;

$dsn = "mysql:host=" .$this->host . ";dbname=" . $this->dbName3 . ";charset=" .$this->charset . "";
$pdo5 = new PDO($dsn, $this->userName3,$this->userPass3, $this->opt); return$pdo4;
}

public function connnect5($userName,$userPass)
{
$this->userName4 =$userName;
$this->userPass4 =$userPass;

$dsn = "mysql:host=" .$this->host . ";dbname=" . $this->dbName4 . ";charset=" .$this->charset . "";
$pdo3 = new PDO($dsn, $this->userName4,$this->userPass4, $this->opt); return$pdo5;
}
}


This is not how things are done in the programming world. You don't write a dozen functions doing the same thing. You write just one function.

Speaking of OOP, there is no use for it here. Given your class consists of just one method, just make it a function:

function connnect($dbName,$userName, $userPass) {$host = 'localhost';
$charset = 'utf8mb4';$opt = [
PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES   => false,
];
$dsn = "mysql:host=$host;dbname=$dbName;charset=$charset";
return new PDO($dsn,$userName, $userPass,$opt);
}


$dbRead = connnect(NAME1, USER2, PASS2);$dbWrte = connnect(NAME1, USER1, PASS1);
$db3 = connnect(NAME3, USER2, PASS2);$db4 = connnect(NAME4, USER3, PASS3);