嘿伙计们我对pdo很新,所以我基本上只是使用我正在阅读的介绍性书籍中的信息组建一个简单的连接类,但这种连接是否有效?如果有人有任何信息性建议,我将非常感激。
class PDOConnectionFactory{
public $con = null;
// swich database?
public $dbType = "mysql";
// connection parameters
public $host = "localhost";
public $user = "user";
public $senha = "password";
public $db = "database";
public $persistent = false;
// new PDOConnectionFactory( true ) <--- persistent connection
// new PDOConnectionFactory() <--- no persistent connection
public function PDOConnectionFactory( $persistent=false ){
// it verifies the persistence of the connection
if( $persistent != false){ $this->persistent = true; }
}
public function getConnection(){
try{
$this->con = new PDO($this->dbType.":host=".$this->host.";dbname=".$this->db, $this->user, $this->senha,
array( PDO::ATTR_PERSISTENT => $this->persistent ) );
// carried through successfully, it returns connected
return $this->con;
// in case that an error occurs, it returns the error;
}catch ( PDOException $ex ){ echo "We are currently experiencing technical difficulties. ".$ex->getMessage(); }
}
// close connection
public function Close(){
if( $this->con != null )
$this->con = null;
}
}
答案 0 :(得分:2)
你可能会发现这篇文章很有用,它是由Erik Wurzer发布的,它发布在Nettuts中,这是我最喜欢的网站之一(除此之外)。
Why you Should be using PHP’s PDO for Database Access
希望这有帮助。
答案 1 :(得分:2)
实现“工厂”时,通常使用它的其他类,方法等不必知道或关心连接,用户名,密码等。
我会做更像的事情:
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();
}
}
}
然后以任何您需要的方式使用CreateNewConnection()返回的连接。
我没有检查上面的代码是否编译,可能会有一些拼写错误/问题,但你明白了。现在你需要更进一步,实现类似于存储库模式的东西:)
答案 2 :(得分:2)
我的建议是你实现一个单例来限制PDO实例化到一个单独的对象。它可能看起来像这样:
class Database {
protected static $_instance;
protected $_connection;
protected $_dns = 'mysql:host=localhost;dbname=mydbname';
protected $_username = 'myusername';
protected $_password = 'mypassword';
/**
* Singleton pattern implementation makes "new" unavailable
*/
protected function __construct()
{
$this->_connection =
new PDO($this->_dns, $this->_username, $this->_password);
}
public function getConnection()
{
return $this->_connection;
}
public static function getInstance()
{
if (null === self::$_instance) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
* Singleton pattern implementation makes "clone" unavailable
*/
protected function __clone()
{}
}
$dbc = Database::getInstance()->getConnection();