单例模式
1 <?php 2 3 class Singleton 4 { 5 private static $_instance; 6 7 /** 8 * 构造函数私有,不允许在外部实例化 9 */ 10 private function __construct() 11 { 12 13 } 14 15 /** 16 * 防止对象实例被克隆 17 */ 18 private function __clone() 19 { 20 21 } 22 23 /** 24 * 防止被反序列化 25 */ 26 private function __wakeup() 27 { 28 29 } 30 31 public static function getInstance() 32 { 33 if (!self::$_instance instanceof self) { 34 self::$_instance = new Singleton(); 35 } 36 37 return self::$_instance; 38 } 39 40 public function test() 41 { 42 echo "This is test"; 43 } 44 } 45 46 47 $a = Singleton::getInstance(); 48 49 echo gettype($a); 50 echo "<br/>"; 51 $a->test(); 52 53 // $b = clone $a; 54 // $c = new Singleton(); 55 56 $d = serialize($a); 57 $e = unserialize($d);