单态
1 <?php 2 //单例 3 class Db{ 4 //创建静态私有的变量保存该类对象 5 static private $instance; 6 //参数 7 static private $config; 8 //防止直接创建对象 9 private function __construct($config){ 10 self::$config = $config; 11 echo 'new obj'; 12 } 13 //防止克隆对象 14 private function __clone(){ 15 return false; 16 } 17 static public function getInstance($config){ 18 //判断$instance是否是Uni的对象 19 if (!self::$instance instanceof self) { 20 //没有则创建 21 self::$instance = new static($config); 22 } 23 return self::$instance; 24 25 } 26 public function getConfig(){ 27 return $this -> config; 28 } 29 } 30 $data = Db::getInstance(1); 31 var_dump($data -> getConfig()); 32 ?>