分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 前端开发

PHP设计模式 - 单例模式

发布时间:2023-09-06 02:01责任编辑:蔡小小关键词:PHP

Singleton(单例模式):单例模式是最常见的模式之一,在Web应用的开发中,常常用于允许在运行时为某个特定的类创建仅有一个可访问的实例。

<?php/** * Singleton class[单例模式] * @author ITYangs<ityangs@163.com> */final class Mysql{ ???/** ????* ????* @var self[该属性用来保存实例] ????*/ ???private static $instance; ???/** ????* ????* @var mixed ????*/ ???public $mix; ???/** ????* Return self instance[创建一个用来实例化对象的方法] ????* ????* @return self ????*/ ???public static function getInstance() ???{ ???????if (! (self::$instance instanceof self)) { ???????????self::$instance = new self(); ???????} ???????return self::$instance; ???} ???/** ????* 构造函数为private,防止创建对象 ????*/ ???private function __construct() ???{} ???/** ????* 防止对象被复制 ????*/ ???private function __clone() ???{ ???????trigger_error(‘Clone is not allowed !‘); ???}}// @test$firstMysql = Mysql::getInstance();$secondMysql = Mysql::getInstance();$firstMysql->mix = ‘ityangs_one‘;$secondMysql->mix = ‘ityangs_two‘;print_r($firstMysql->mix);// 输出: ityangs_twoprint_r($secondMysql->mix);// 输出: ityangs_two

在很多情况下,需要为系统中的多个类创建单例的构造方式,这样,可以建立一个通用的抽象父工厂方法:

<?php/** * Singleton class[单例模式:多个类创建单例的构造方式] * @author ITYangs<ityangs@163.com> */abstract class FactoryAbstract { ???protected static $instances = array(); ???public static function getInstance() { ???????$className = self::getClassName(); ???????if (!(self::$instances[$className] instanceof $className)) { ???????????self::$instances[$className] = new $className(); ???????} ???????return self::$instances[$className]; ???} ???public static function removeInstance() { ???????$className = self::getClassName(); ???????if (array_key_exists($className, self::$instances)) { ???????????unset(self::$instances[$className]); ???????} ???} ???final protected static function getClassName() { ???????return get_called_class(); ???} ???protected function __construct() { } ???final protected function __clone() { }}abstract class Factory extends FactoryAbstract { ???final public static function getInstance() { ???????return parent::getInstance(); ???} ???final public static function removeInstance() { ???????parent::removeInstance(); ???}}// @testclass FirstProduct extends Factory { ???public $a = [];}class SecondProduct extends FirstProduct {}FirstProduct::getInstance()->a[] = 1;SecondProduct::getInstance()->a[] = 2;FirstProduct::getInstance()->a[] = 11;SecondProduct::getInstance()->a[] = 22;print_r(FirstProduct::getInstance()->a);// Array ( [0] => 1 [1] => 11 )print_r(SecondProduct::getInstance()->a);// Array ( [0] => 2 [1] => 22 )

PHP设计模式 - 单例模式

原文地址:https://www.cnblogs.com/taozi32/p/9226441.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved