3
\$\begingroup\$

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;
  }
}
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

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);
}

and then create your connections:

$dbRead = connnect(NAME1, USER2, PASS2);
$dbWrte = connnect(NAME1, USER1, PASS1);
$db3 = connnect(NAME3, USER2, PASS2);
$db4 = connnect(NAME4, USER3, PASS3);
$db5 = connnect(NAME5, USER4, PASS4);
\$\endgroup\$
4
  • \$\begingroup\$ Thanks you. I do feel like a bit of an idiot now you've made it that simple and I understand that a class isn't necessary but I was just looking for some advice on the best practices of handling multiple connections using OOP PHP. I realise I should just use a single function or method now. I find some of the concepts of OOP a bit strange so I would like to practice it to try and understand it more. I've watched some tutorials on OOP and MVC pattern so I was trying to keep everything separate so that's why my plan was to use a class to handle the connections. \$\endgroup\$ Commented Nov 16, 2018 at 13:05
  • \$\begingroup\$ Speaking of multiple connections, there is nothing much OOP can help you with. You just need multiple instances of PDO, and how do you get them doesn't make too much difference. OOP is more about organizing the code, but here there is not so much to organize \$\endgroup\$ Commented Nov 16, 2018 at 13:12
  • \$\begingroup\$ Alright, well I'll take that onboard. Thanks again for your feedback. \$\endgroup\$ Commented Nov 16, 2018 at 13:22
  • \$\begingroup\$ OOP will be involved in how you're going to use these connections though. \$\endgroup\$ Commented Nov 16, 2018 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.