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.
嘿,伙计们,我对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;
}
}
}
3 个解决方案
#1
2
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.
然后以任何需要的方式使用CreateNewConnection()返回的连接。
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 :)
我没有检查上面的代码是否编译,可能会有一些错误/问题,但是您应该明白了。现在您需要更进一步,实现类似存储库模式的内容:)
#2
2
My suggestion is that you implement a singleton to restrict the instantiation of PDO to one single object. It might look like this:
我的建议是实现一个单例,将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();
#3
2
You might find this article useful, it's by Erik Wurzer and it was posted in Nettuts, which is one of my other favorite websites (besides this).
Why you Should be using PHP’s PDO for Database Access
你可能会发现这篇文章很有用,它是埃里克·沃泽尔(Erik Wurzer)写的,被贴在Nettuts上,这是我最喜欢的网站之一(除了这个)。为什么要使用PHP的PDO进行数据库访问
Hope this helps.
希望这个有帮助。
#1
2
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.
然后以任何需要的方式使用CreateNewConnection()返回的连接。
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 :)
我没有检查上面的代码是否编译,可能会有一些错误/问题,但是您应该明白了。现在您需要更进一步,实现类似存储库模式的内容:)
#2
2
My suggestion is that you implement a singleton to restrict the instantiation of PDO to one single object. It might look like this:
我的建议是实现一个单例,将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();
#3
2
You might find this article useful, it's by Erik Wurzer and it was posted in Nettuts, which is one of my other favorite websites (besides this).
Why you Should be using PHP’s PDO for Database Access
你可能会发现这篇文章很有用,它是埃里克·沃泽尔(Erik Wurzer)写的,被贴在Nettuts上,这是我最喜欢的网站之一(除了这个)。为什么要使用PHP的PDO进行数据库访问
Hope this helps.
希望这个有帮助。