请问哪一段代码保证了不管new多少次都只有一个数据库连接(即所谓的单例模式)
有没有大神能够解释一下
有没有大神能够解释一下
2015-09-15
class User {
//静态变量保存全局实例
private static $_instance = null;
//私有构造函数,防止外界实例化对象
private function __construct() {
}
//静态方法,单例统一访问入口
static public function getInstance() {
if (is_null ( self::$_instance ) || isset ( self::$_instance )) {
self::$_instance = new self ();
}
return self::$_instance;
}
}使用单例模式可以避免大量的new操作。因为每一次new操作都会消耗系统和内存的资源。
举报