Blogger Information
Blog 11
fans 0
comment 0
visits 6638
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP设计模式学习笔记——单例模式
青Blue的博客
Original
625 people have browsed it

实例

<?php
/**
* @                       .::::.
* @                     .::::::::.
* @                    :::::::::::
* @                 ..:::::::::::'
* @              '::::::::::::'
* @                .::::::::::
* @           '::::::::::::::..
* @                ..::::::::::::.
* @              ``::::::::::::::::
* @               ::::``:::::::::'        .:::.
* @              ::::'   ':::::'       .::::::::.
* @            .::::'      ::::     .:::::::'::::.
* @           .:::'       :::::  .:::::::::' ':::::.
* @          .::'        :::::.:::::::::'      ':::::.
* @         .::'         ::::::::::::::'         ``::::.
* @     ...:::           ::::::::::::'              ``::.
* @    ````':.          ':::::::::'                  ::::..
* @                       '.:::::'                    ':'````..
* @
* @Description: 单例模式,用在数据库连接,cookie操作,读取配置文件等
* @Author: luoxiaojin
* @Date: 2020-06-29 14:50:10
* @LastEditors: luoxiaojin
* @LastEditTime: 2020-06-29 14:50:51
* @FilePath: \design_patterns\l4.php
*/

class Sigle{
    private static $obj = null;
    final private function __construct(){
        // 
    }

    final private function __clone(){
        // 
    }
    
    public static function init(){
        if(self::$obj !== null){
            return self::$obj;
        }else{
            self::$obj = new self();
            return self::$obj;
        }
    }
}

$s1 = Sigle::init();
// var_dump($s1);
$s2 = Sigle::init();



echo $s1 === $s2;

运行实例 »

点击 "运行实例" 按钮查看在线实例

——学习参考与 bilibili燕十八 面向对象与设计模式

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post