PHP面试题:单例模式,创建mysqli数据库链接的单例对象

2022-11-29 10:11:35 浏览数 (1)

代码语言:javascript复制
class Db { 
 private static $instance; 
 public $handle; 
 Private function __construct($host,$username,$password,$dbname) { 
  $this->handle=NULL; 
  $this->getcon($host,$username,$password,$dbname); 
 } 
 public static function getBb() { 
  self::$instance=new Db(); 
  return self::$instance; 
 } 
 private function getcon($host,$username,$password,$dbname) { 
  if($this->handle!=NULL){ 
   return true; 
  } 
  $this->handle=mysqli_connect($host,$username,$password,$dbname); 
 } 
}

0 人点赞