Creating the Singleton design pattern in PHP5

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

How would one create a Singleton class using PHP5 classes?

21条回答
  •  旧时难觅i
    2020-11-22 05:10

    Quick example:

    final class Singleton
    {
        private static $instance = null;
    
        private function __construct(){}
    
        private function __clone(){}
    
        private function __wakeup(){}
    
        public static function get_instance()
        {
            if ( static::$instance === null ) {
                static::$instance = new static();
            }
            return static::$instance;
        }
    }
    

    Hope help.

提交回复
热议问题