Skip to main content
update wording
Source Link

This is not how things are done in the programming world. You don't write a dozen of 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 functionmethod, 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);

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

Speaking of OOP, there is no use for it here. Given your class consists of just one function, 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);

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);
Source Link
Your Common Sense
  • 9.1k
  • 1
  • 22
  • 51

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

Speaking of OOP, there is no use for it here. Given your class consists of just one function, 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);