<?php
    class Singleton
    {
        public $name;
        /********** Begin **********/
        /********** 1. 单例模式的类只能有一个实例,并由类自己创建(此处略) **********/
        public static $instance;
 
        /********** 2. 构造函数必须为私有 **********/
 
        private function __construct(){
    }
 
        /********** 3. 克隆函数必须为私有 **********/
        private function __clone(){
       
        }
        /********** 4. 必须提供一个静态的访问方法 **********/
        public static function getInstance()
        {
            if(!(self::$instance instanceof self)){
                self::$instance = new self();
            }
            return self::$instance;
        }
        /********** End **********/
    }

更多推荐