daily pastebin goal
26%
SHARE
TWEET

Untitled

a guest May 16th, 2018 103 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. namespace DesignPatterns\Creational\Singleton;
  4.  
  5. /**
  6.  * Singleton类
  7.  */
  8. class Singleton
  9. {
  10.     /**
  11.      * @var Singleton reference to singleton instance
  12.      */
  13.     private static $instance;
  14.    
  15.     /**
  16.      * 通过延迟加载(用到时才加载)获取实例
  17.      *
  18.      * @return self
  19.      */
  20.     public static function getInstance()
  21.     {
  22.         if (null === static::$instance) {
  23.             static::$instance = new static;
  24.         }
  25.  
  26.         return static::$instance;
  27.     }
  28.  
  29.     /**
  30.      * 构造函数私有,不允许在外部实例化
  31.      *
  32.      */
  33.     private function __construct()
  34.     {
  35.     }
  36.  
  37.     /**
  38.      * 防止对象实例被克隆
  39.      *
  40.      * @return void
  41.      */
  42.     private function __clone()
  43.     {
  44.     }
  45.  
  46.     /**
  47.      * 防止被反序列化
  48.      *
  49.      * @return void
  50.      */
  51.     private function __wakeup()
  52.     {
  53.     }
  54. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top