当前位置:网站首页>Detailed explanation of PHP singleton mode
Detailed explanation of PHP singleton mode
2022-06-24 09:56:00 【BigChen_ up】
The singleton pattern :
It can prevent users from multiple instantiation operations , Instantiate only once ! Also called a single instance
- Users are not allowed to instantiate : Protective construction method new
- Provide a static method , A single instance can be generated
- Users are not allowed clone operation . Protect __clone The method can
- Static attribute , Used to save a single instance
The singleton pattern 3 private 1 Public principle :
Private structure
Private clone
Private Static attribute
Open Static methods
Take the database class as an example :
- Let's start with a wrong example :
class DB {
// Construction method : new
public function __construct() {
echo ' Connect to database <br>';
}
public function __destruct() {
echo ' Close the database <br>';
}
public function fetchAll() {
echo ' Query operation !<br>';
}
}
// Normal human practice
$db = new DB();
$arr1 = $db->fetchAll();
$arr2 = $db->fetchAll();
$arr3 = $db->fetchAll();
$arr4 = $db->fetchAll();
$arr5 = $db->fetchAll();
echo '<hr>';
// Xiao Ming's practice :
// Xiao Ming thinks , new The object of Only one query operation can be performed
$db1 = new DB();
$db1->fetchAll();
$db2 = new DB();
$db2->fetchAll();
$db3 = new DB();
$db3->fetchAll();
$db4 = new DB();
$db4->fetchAll();
The following uses the singleton pattern to prevent the class from being instantiated multiple times :
class DB {
// Static attribute :
private static $instance = null;
//Instance example
//$a = DB::getInstance()
//$b = DB::getInstance()
//$c = DB::getInstance()
// Multiple calls , Only for the first time will I enter if Judge , Will instantiate . When called again , because $instance It's worth it , So I won't go in again if, It will not be instantiated again
public static function getInstance() {
if (self::$instance == null) {
self::$instance = new self;
}
return self::$instance;
}
// Construction method : new
private function __construct() {
echo ' Connect to database <br>';
}
// Private clone
//
private function __clone() {
# code...
}
public function __destruct() {
echo ' Close the database <br>';
}
public function fetchAll() {
echo ' Query operation !<br>';
}
}
$db1 = DB::getInstance();
$db1->fetchAll();
边栏推荐
- 顶刊TPAMI 2022!基于不同数据模态的行为识别:最新综述
- Mise en œuvre du rendu de liste et du rendu conditionnel pour l'apprentissage des applets Wechat.
- Symbol. Iterator iterator
- How to improve the efficiency of network infrastructure troubleshooting and bid farewell to data blackouts?
- 什么情况下应该使用GridFS?
- Idea cannot save settings source root d:xxxx is duplicated in module XXX
- 医学图像开源数据集汇总(二)
- Implementation of simple floating frame in WindowManager
- 2021-08-17
- R 椭圆随机点产生并画图
猜你喜欢

文献调研报告

415-二叉树(144. 二叉树的前序遍历、145. 二叉树的后序遍历、94. 二叉树的中序遍历)

5分钟,客服聊天处理技巧,炉火纯青

How to improve the efficiency of network infrastructure troubleshooting and bid farewell to data blackouts?

Prct-1400: failed to execute getcrshome resolution

Five heart matchmaker

Oracle数据文件头SCN不一致处理方法

微信小程序學習之 實現列錶渲染和條件渲染.

如何提高网络基础设施排障效率,告别数据断档?

indexedDB本地存储,首页优化
随机推荐
Why is LNX of e equal to X
Oracle viewing data file header SCN information
415-二叉树(144. 二叉树的前序遍历、145. 二叉树的后序遍历、94. 二叉树的中序遍历)
PTA猴子选大王(约瑟夫环问题)
CICFlowMeter源码分析以及为满足需求而进行的修改
数组无缝滚动demo
二叉樹第一部分
js代理模式
Servlet fast foundation building
How to locate lock waiting in Dameng database
Honeypot 2 hfish, ehoney
算法---矩阵中战斗力最弱的 K 行(Kotlin)
PTA monkey chooses King (Joseph Ring problem)
Implementation of simple floating frame in WindowManager
Algorithm - the K row with the weakest combat power in the matrix (kotlin)
十大证券公司哪个佣金最低,最安全可靠?有知道的吗
JS proxy mode
Symbol.iterator 迭代器
Use of vim
【Eureka注册中心】