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

    单例模式主要用于解决一个全局使用的类频繁地创建与销毁的问题,可以节省系统资源。特征是将构造函数设置为私有,主要用于数据库连接、系统产生唯一序列号

    <?php
    
    
    namespace DesignModel;
    
    
    /**
     * 单例模式
     * Class SingletonClass
     * @package DesignModel
     */
    final class SingletonClass
    {
        /**
         * @var SingletonClass
         */
        private static $instance;
    
        /**
         * 不能外部实例化,只能通过 SingletonClass::getInstance() 实现
         * SingletonClass constructor.
         */
        private function __construct()
        {
        }
    
        /**
         * 通过懒加载获得实例
         * @return SingletonClass
         */
        public static function getInstance(): SingletonClass
        {
            if (null === static::$instance) {
                static::$instance = new static();
            }
    
            return static::$instance;
        }
    
        /**
         * 防止实例被克隆
         */
        private function __clone()
        {
        }
    
        /**
         * 防止反序列化
         */
        private function __wakeup()
        {
        }
    }
    
  • 相关阅读:
    vim配置文件解析
    VIM使用技巧5
    补不manjaro系统
    linux下终端录制
    VIM的修炼等级
    VIM使用技巧4
    64位linux 汇编
    linux下编译安装gcc5.1
    Git学习笔记
    HTML实体符号代码速查表
  • 原文地址:https://www.cnblogs.com/it-abel/p/11070368.html
Copyright © 2011-2022 走看看