实例
<?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燕十八 面向对象与设计模式