Any suggestions to improve my PDO connection class?
When implementing a "Factory" usually it is so that other classes, methods, etc using it don't have to know or care about the connections, usernames, passwords, etc.
I would do it something more like:
static class PDOConnectionFactory {
// database
private $dbType = "mysql";
// connection parameters
private $host = "localhost";
private $user = "user";
private $senha = "password";
private $db = "database";
// new CreateNewConnection( true ) <--- persistent connection
// new CreateNewConnection() <--- no persistent connection
public function CreateNewConnection($persistent = false) {
try {
$con = new PDO($dbType . ":host=" . $host . ";dbname=" . $db, $user, $senha, array(PDO::ATTR_PERSISTENT => $persistent));
// carried through successfully, it returns connected
return $con;
}
catch (PDOException $ex) {
// in case that an error occurs, it returns the error;
echo "We are currently experiencing technical difficulties. We have a bunch of monkies working really hard to fix the problem. Check back soon: " . $ex->getMessage();
}
}
}
Then you use the connection returned by CreateNewConnection() in whatever way you need.
I didn't check if the above code compiles, there could be a few typos/issues, but you get the idea. Now you need to take it a step further and implement something like the repository pattern :)