当前位置:网站首页>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();
边栏推荐
- 如何让社交媒体成为跨境电商驱动力?这款独立站工具不能错过!
- Geogebra instance clock
- PTA monkey chooses King (Joseph Ring problem)
- Summary of medical image open source datasets (II)
- Oracle database listening file configuration
- Threejs MMD model loading + contour loading + animation loading + Audio loading + camera animation loading +ammojs loading gltf model loading +gltf reflection adjustment
- Jcim | AI based protein structure prediction in drug discovery: impacts and challenges
- 416-二叉树(前中后序遍历—迭代法)
- LeetCode: 240. Search 2D matrix II
- Oracle查看数据文件头SCN信息
猜你喜欢

PTA猴子选大王(约瑟夫环问题)

Practical analysis: implementation principle of APP scanning code landing (app+ detailed logic on the web side) with source code

Why is LNX of e equal to X

Top issue tpami 2022! Behavior recognition based on different data modes: a recent review

Handling method of Oracle data file header SCN inconsistency

五心红娘

ssh远程免密登录

Reasons for the failure of digital transformation and the way to success

小程序学习之获取用户信息(getUserProfile and getUserInfo)

LeetCode: 240. Search 2D matrix II
随机推荐
【自定义Endpoint 及实现原理】
414-二叉树的递归遍历
Prct-1400: failed to execute getcrshome resolution
PostgreSQL DBA quick start - source compilation and installation
《MATLAB 神经网络43个案例分析》:第32章 小波神经网络的时间序列预测——短时交通流量预测
Indexeddb local storage, homepage optimization
Basic operations on binary tree
How to solve multi-channel customer communication problems in independent stations? This cross-border e-commerce plug-in must be known!
数字化转型的失败原因及成功之道
impdp导schema报ORA-31625异常处理
Ggplot2 color setting summary
买的长期理财产品,可以转短吗?
PHP file lock
新手怎么选择投资理财产品的等级?
Oracle database expdp only exports tables
五心红娘
如何解决独立站多渠道客户沟通难题?这款跨境电商插件一定要知道!
Honeypot 2 hfish, ehoney
Ora-28000 error after upgrading Oracle 12C to 19C
Open Oracle server under Linux to allow remote connection