标签:
class Instance { //静态变量保存全局实例 private static $_instance = null; //私有构造函数,防止外界实例化对象 private function __construct() { echo ‘看我出现几次,就说明我被创建了几次<hr>‘; } //私有克隆函数,防止外办克隆对象 private function __clone() {} //静态方法,单例模式统一访问入口 static public function getInstance() { if (is_null(self::$_instance)) { self::$_instance = new self(); } return self::$_instance; } } Instance::getInstance(); Instance::getInstance(); Instance::getInstance();
标签:
原文地址:http://www.cnblogs.com/cloudshadow/p/php-singleton.html