有什么建议可以改善我的PDO连接等级吗? [英] Any suggestions to improve my PDO connection class?
问题描述
伙计们,我对pdo还是很陌生,所以我基本上只是使用我正在阅读的入门书中的信息来组合一个简单的连接类,但是这种连接有效吗?如果有人有任何有益的建议,我将不胜感激.
Hey guys I am pretty new to pdo so I basically just put together a simple connection class using information out of the introductory book I was reading but is this connection efficient? If anyone has any informative suggestions, I would really appreciate it.
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;
}
}
推荐答案
在实现工厂"时,通常是这样,以便使用它的其他类,方法等不必知道或关心连接,用户名,密码等
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();
}
}
}
然后,您可以根据需要使用CreateNewConnection()返回的连接.
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 :)
这篇关于有什么建议可以改善我的PDO连接等级吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!