单例模式:只有一个实例,不能在new出多个,不能克隆出多个
<?php class Mysql{ //该属性用来保存实例 private static $conn; //构造函数为private,防止创建对象 private function __construct(){ self::$conn = mysqli_connect('localhost','root','root'); } //创建一个用来实例化对象的方法 public static function getInstance(){ if(!(self::$conn instanceof self)){ self::$conn = new self; } return self::$conn; } public static function shu(){ echo self::$ceshi; } //防止对象被复制 public function __clone(){ trigger_error('Clone is not allowed !'); } } $mysql = Mysql::getInstance(); ?>
参考:http://blog.csdn.net/jungsagacity/article/details/7618587