<?php /** * */ class DB { public $link; private static $db;//看当前这个类有没有被实例化 private function __construct($host,$user,$pwd,$dbname){ $this->link = mysqli_connect($host,$user,$pwd,$dbname); } private function __clone(){} //单例模式的核心思想:自己调用自己 public static function connect_mysql($host,$user,$pwd,$dbname){ if(!(self::$db instanceof self)){ self::$db = new self($host,$user,$pwd,$dbname); } return self::$db; } } $obj = DB::connect_mysql('127.0.0.1','root','root','1812a'); print_r($obj);