可以检查和查看对象是否已经用php实例化了吗?我有一个mysql类,其他页面实例化它来使用它的方法,我所做的是记录所有查询的日志。我注意到mysql一次打开3-4次,而不是一次。因此,我需要验证对象是否已实例化,如果没有,它将不会创建另一个对象并打开另一个无用的连接。
发布于 2011-08-02 09:03:12
听起来像是单例模式的完美案例。
class Connection
{
/**
* @var Connection
*/
private static $instance;
private static $config = array();
private function __construct()
{
// whatever you need in here, just keep the method private
}
public function __clone()
{
throw new RuntimeException;
}
public function __wakeup()
{
throw new RuntimeException;
}
public static setConfig(array $config)
{
self::$config = $config;
}
public static function getInstance()
{
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
}发布于 2011-08-02 09:02:33
您正在寻找singleton
发布于 2011-08-02 09:03:10
您可以使用instanceof操作符检查给定的变量是否属于特定的类:
if ($db instanceof DatabaseClass) :点击此处了解更多信息:http://php.net/instanceof
https://stackoverflow.com/questions/6906203
复制相似问题