如何在 php 单例 class 中关闭 PDO 连接
How to close the PDO connection in php singleton class
我有一个 PDO 的单例 class,下面是 class
的相关部分
public static function getInstance( $config )
{
if ( ! isset(self::$instance))
self::$instance = new self( $config );
return self::$instance;
}
public function __construct( $config )
{
self::$start = self::timer();
try
{
$host = $config['host'];
$dbname = $config['name'];
$user = $config['user'];
$password = $config['password'];
self::$objInstance = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8",
"$user",
"$password",
array(PDO::ATTR_PERSISTENT => true)
);
self::$objInstance -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
}
catch (PDOException $e)
{
//need to log for security
die('PDO CONNECTION ERROR: ' . $e->getMessage() . '<br/>');
}
}
public static function closeConnection ( )
{
try
{
self::$objInstance = null;
}
catch (PDOException $e)
{
//OPTIMIZE**************************** to do
//file_put_contents("log/dberror.log", "Date: " . date('M j Y - G:i:s') . " ---- Error: " . $e->getMessage().PHP_EOL, FILE_APPEND);
die($e->getMessage());
}
self::$end = self::timer();
}
所以这里一切正常,但是当我在任何数据库操作之后调用静态 closeConnection()
时,它使 PDO 实例为空,这很好但是再次调用 PDO 实例会出错,因为 PDO 现在是null 由于调用 closeconnection。我的问题是如何处理关闭单例 class 中的 PDO?
改变
self::$objInstance = null;
进入
self::$instance = null;
因为您尝试删除 PDO 对象而不是单例对象。
Upon successful connection to the database, an instance of the PDO
class is returned to your script. The connection remains active for
the lifetime of that PDO object. To close the connection, you need to
destroy the object by ensuring that all remaining references to it are
deleted--you do this by assigning NULL
to the variable that holds the
object. If you don't do this explicitly, PHP will automatically close
the connection when your script ends.
对于单例,连接的整个生命周期应该由单例本身控制。因此,我会依赖记录的自动关闭连接,而不公开关闭连接的方法(出于同样的原因,不应公开构造函数)。
我有一个 PDO 的单例 class,下面是 class
的相关部分 public static function getInstance( $config )
{
if ( ! isset(self::$instance))
self::$instance = new self( $config );
return self::$instance;
}
public function __construct( $config )
{
self::$start = self::timer();
try
{
$host = $config['host'];
$dbname = $config['name'];
$user = $config['user'];
$password = $config['password'];
self::$objInstance = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8",
"$user",
"$password",
array(PDO::ATTR_PERSISTENT => true)
);
self::$objInstance -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
}
catch (PDOException $e)
{
//need to log for security
die('PDO CONNECTION ERROR: ' . $e->getMessage() . '<br/>');
}
}
public static function closeConnection ( )
{
try
{
self::$objInstance = null;
}
catch (PDOException $e)
{
//OPTIMIZE**************************** to do
//file_put_contents("log/dberror.log", "Date: " . date('M j Y - G:i:s') . " ---- Error: " . $e->getMessage().PHP_EOL, FILE_APPEND);
die($e->getMessage());
}
self::$end = self::timer();
}
所以这里一切正常,但是当我在任何数据库操作之后调用静态 closeConnection()
时,它使 PDO 实例为空,这很好但是再次调用 PDO 实例会出错,因为 PDO 现在是null 由于调用 closeconnection。我的问题是如何处理关闭单例 class 中的 PDO?
改变
self::$objInstance = null;
进入
self::$instance = null;
因为您尝试删除 PDO 对象而不是单例对象。
Upon successful connection to the database, an instance of the PDO class is returned to your script. The connection remains active for the lifetime of that PDO object. To close the connection, you need to destroy the object by ensuring that all remaining references to it are deleted--you do this by assigning
NULL
to the variable that holds the object. If you don't do this explicitly, PHP will automatically close the connection when your script ends.
对于单例,连接的整个生命周期应该由单例本身控制。因此,我会依赖记录的自动关闭连接,而不公开关闭连接的方法(出于同样的原因,不应公开构造函数)。