Creating the Singleton design pattern in PHP5

前端 未结 21 2043
猫巷女王i
猫巷女王i 2020-11-22 04:21

How would one create a Singleton class using PHP5 classes?

21条回答
  •  既然无缘
    2020-11-22 05:14

    This should be the right way of Singleton.

    class Singleton {
    
        private static $instance;
        private $count = 0;
    
        protected function __construct(){
    
        }
    
        public static function singleton(){
    
            if (!isset(self::$instance)) {
    
                self::$instance = new Singleton;
    
            }
    
            return self::$instance;
    
        }
    
        public function increment()
        {
            return $this->count++;
        }
    
        protected function __clone(){
    
        }
    
        protected function __wakeup(){
    
        }
    
    } 
    

提交回复
热议问题