<?php class DB{ protected $name = "DB"; private static $pattern; private $db; //数据库连接参数 const HOSTNAME = "127.0.0.1"; const USERNAME = "root"; const PASSWORD = "root"; const DBNAME = "wwwlwcom"; //构造函数 private function __construct(){ //连接数据库 $this->db = mysqli_connect(self::HOSTNAME,self::USERNAME,self::PASSWORD,self::DBNAME); } //魔术克隆方法 protected function __clone() { // TODO: Implement __clone() method. } public static function getPattern(){ if(!(self::$pattern instanceof self)){ self::$pattern = new self(); } return self::$pattern; } //数据库查询 public function getInfo(){ $sql = "select * from lw_fiction"; $res = mysqli_query($this->db,$sql); while($row = mysqli_fetch_array($res)){ echo ($row[‘cover‘]."?<br/>".$row[‘title‘]."?<br/>".$row[‘author‘]); } mysqli_free_result($res); } } $demo = DB::getPattern(); $demo->getInfo();