Best practices for a PDO class? [closed]
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this questionI 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?
Singleton will bite you in the ass the first time you need to connect to two different databases, either for replication or two different databases. Then your code is so messed up.
Instead, utilize a single static function to easily load the last used configuration:
class MyPDODB extends PDO
{
// Prevent unconfigured PDO instances!
private function __construct($config)
{
$dsn = sprintf('mysql:dbname=%s;host=%s;port=%d;', $config['database'], $config['hostname'], $config['port']);
parent::__construct($dsn, $config['username'], $config['password']);
}
public static function loadDB($config_in = null)
{
static $last_config = null;
if (!is_null($config_in))
{
self::validateConfig($config_in);
$config = $config_in;
if (!isset($config['isTemp']) || $config['isTemp'] !== true)
{
$last_config = $config;
}
}
else
{
if (!is_null($last_config)) { $config = $last_config; }
else throw new MyDBException("No config provided');
}
return new MyPDODB($config);
}
}
In any function, you just do:
$db = MyPDODB::loadDB();
$db->prepare($sql);
$db->execute();
Easy, huh?
extend PDO to get you better control over it.
class Database Extends PDO
{
static $instance; //singleton
static function Singleton($params = false)
{
if(!isset(self::$instance))
{
self::$instance = new self($params); //tomh
self::$instance->init();
}
return self::$instance;
}
private function __construct(){}; //not allowed with singleton.
public function init($params) //Override PDO::__construct()
{
parent::__construct($params);
}
public function query($query)
{
//Catch,Custom Query Object maybe. W.e
return parent::query($modified_query);
}
}
Usage:
$Database = Database::Singleton(array('user' => 'root')); //....
$Database->query('Helooooooo Sexy MySql, Send me my Shizzle');
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.
精彩评论