zoukankan      html  css  js  c++  java
  • 单例模式

     1 <?php
     2 
     3 class Singleton
     4 {
     5     private static $_instance;
     6 
     7     /**
     8      * 构造函数私有,不允许在外部实例化
     9      */
    10     private function __construct()
    11     {
    12 
    13     }
    14 
    15     /**
    16      * 防止对象实例被克隆
    17      */
    18     private function __clone()
    19     {
    20 
    21     }
    22 
    23     /**
    24      * 防止被反序列化
    25      */
    26     private function __wakeup()
    27     {
    28 
    29     }
    30 
    31     public static function getInstance()
    32     {
    33         if (!self::$_instance instanceof self) {
    34             self::$_instance = new Singleton();
    35         }
    36 
    37         return self::$_instance;
    38     }
    39 
    40     public function test()
    41     {
    42         echo "This is test";
    43     }
    44 }
    45 
    46 
    47 $a = Singleton::getInstance();
    48 
    49 echo gettype($a);
    50 echo "<br/>";
    51 $a->test();
    52 
    53 // $b = clone $a;
    54 // $c = new Singleton();
    55  
    56 $d = serialize($a);
    57 $e = unserialize($d);
    View Code
  • 相关阅读:
    CentOS
    Docker
    Chart的简单使用
    DataGridView中间插入数据行
    获取每个月的固定的第n个星期几
    设置只能开启一个程序实例
    DataContext与实体类
    Attribute
    Delegate
    Event
  • 原文地址:https://www.cnblogs.com/hangtt/p/6255743.html
Copyright © 2011-2022 走看看