I am creating a helper class and adding in a PDO instance into it.
I want the class method to be able to connect to various databases.
What is the best way to go around it?
So far I have:
public static function connect($dbType, $database, $host, $username,
$password, $options = array())
{
switch($dbType)
{
case "pgsql":
$DSN = "pgsql:dbname=$database;host=$host";
break;
case "mysql":
$DSN = "mysql:host=$host;dbname=$database";
break;
case .....
}
}
Is there a better way to perform this without the need to detect the $dbType over and over?
Is there a more dynamic way to do it?
Personally I don’t see the need to provision for so many databases, because PDO doesn’t abstract out the intricate query language differences and capabilities of each driver 🙂
Having said that, you could create a function to just generate the DSN: