I want to create a PDO class for handling data base connections.
Here is what I have:
require('php/packages/store/store_db_settings.php');
class store_pdo
{
private $DBH; // Data Base Handler
function __construct()
{
$DBH = new PDO(DB_DSN,DB_USER,DB_PASSWORD);
}
public function getHandler()
{
return $DBH;
}
}
I think this seems ok, however I am used to just using things like mysql_query and not really sure what problems I could run into in the future. So I thought the experience here could offer guidance.
Is what I have sufficient? should I make my class Singleton, or use static functions? Is there a best practice?
I don’t want to go with this, then I after have several other classes using it, discover that I should have written it differently.
P.S. I just noticed that the best-practices tag is no longer allowed… does that mean questions like this are discouraged now too?
A Singleton and a static class would both work fine. I can’t think of a situation when it would make a difference.
Make sure that if there ever may be the possibility of using multiple connections, you make your Singleton/Static class multi-connection capable from the start (using an array of connections, or object properties…)
It could be argued, though, that all these methods create a “God Object” of some sort, are nothing but a glorified
global, and go against the principles of real OOP. I asked a question about this once that yielded a lot of great feedback.