当前位置:网站首页>php单例模式详解
php单例模式详解
2022-06-24 08:08:00 【BigChen_up】
单例模式:
可以阻止用户多次实例化操作, 只能实例化一次!也叫做单一实例
- 不允许用户自己实例化: 保护构造方法 new
- 提供一个静态方法, 可以生成单一实例
- 不允许用户 clone操作. 保护__clone方法即可
- 静态属性, 用于保存单一实例
单例模式3私1公原则:
私有的 构造
私有的 clone
私有的 静态属性
公开的 静态方法
下面以数据库类为例:
- 先上一个错误的例子:
class DB {
//构造方法: new
public function __construct() {
echo '连接数据库<br>';
}
public function __destruct() {
echo '关闭数据库<br>';
}
public function fetchAll() {
echo '查询操作!<br>';
}
}
//正常人类的做法
$db = new DB();
$arr1 = $db->fetchAll();
$arr2 = $db->fetchAll();
$arr3 = $db->fetchAll();
$arr4 = $db->fetchAll();
$arr5 = $db->fetchAll();
echo '<hr>';
//小明的做法:
//小明认为, new出的对象 只能执行一次查询操作
$db1 = new DB();
$db1->fetchAll();
$db2 = new DB();
$db2->fetchAll();
$db3 = new DB();
$db3->fetchAll();
$db4 = new DB();
$db4->fetchAll();
下面使用单例模式防止类被多次实例化:
class DB {
//静态属性:
private static $instance = null;
//Instance实例
//$a = DB::getInstance()
//$b = DB::getInstance()
//$c = DB::getInstance()
//多次调用, 只有第一次会进入if判断, 会进行实例化操作. 再次调用时, 由于$instance已经有值了, 所以不会再进if, 也就不会再次实例化
public static function getInstance() {
if (self::$instance == null) {
self::$instance = new self;
}
return self::$instance;
}
//构造方法: new
private function __construct() {
echo '连接数据库<br>';
}
//私有的clone
//
private function __clone() {
# code...
}
public function __destruct() {
echo '关闭数据库<br>';
}
public function fetchAll() {
echo '查询操作!<br>';
}
}
$db1 = DB::getInstance();
$db1->fetchAll();
边栏推荐
- 深入了解 border
- 零基础自学SQL课程 | 子查询
- The border problem after the focus of input
- Depens:*** but it is not going to be installed
- Squid代理服务器应用
- [ES6 breakthrough] promise is comparable to native custom encapsulation (10000 words)
- [noi Simulation Competition] send (tree DP)
- P6117-[JOI 2019 Final]コイン集め【贪心】
- 4274. suffix expression
- 零基础自学SQL课程 | 相关子查询
猜你喜欢

Mba-day25 best value problem - application problem

YOLOX backbone——CSPDarknet的实现

目标检测系列——Fast R-CNN
![[Niuke] convert string to integer](/img/56/3e491b3d0eea0d89a04d0b871501d7.png)
[Niuke] convert string to integer

The list of open source summer winners has been publicized, and the field of basic software has become a hot application this year

How to configure environment variables and distinguish environment packaging for multi terminal project of uniapp development

Zero foundation self-study SQL course | related sub query

Redis实现全局唯一ID
![[noi Simulation Competition] send (tree DP)](/img/5b/3beb9f5fdad00b6d5dc789e88c6e98.png)
[noi Simulation Competition] send (tree DP)

解决:jmeter5.5在win11下界面上的字特别小
随机推荐
Applet wx show
Numpy NP in numpy c_ And np r_ Explain in detail
Data midrange: detailed explanation of the technical stack of data acquisition and extraction
Huawei Router: IPSec Technology
NETRCA: AN EFFECTIVE NETWORK FAULT CAUSE LOCALIZATION之论文阅读
How to configure environment variables and distinguish environment packaging for multi terminal project of uniapp development
Zero foundation self-study SQL course | syntax sequence and execution sequence of SQL statements
Niuke network decimal integer to hexadecimal string
Cdga | how can we do well in data governance?
每周推薦短視頻:談論“元宇宙”要有嚴肅認真的態度
Framework tool class obtained by chance for self use
Time Series Data Augmentation for Deep Learning: A Survey 之论文阅读
【Redis實現秒殺業務①】秒殺流程概述|基本業務實現
How to import MDF and LDF files into MySQL workbench
When programmers are asked if they can repair computers... | daily anecdotes
YOLOX backbone——CSPDarknet的实现
Data middle office: detailed explanation of technical architecture of data middle office
L01_一条SQL查询语句是如何执行的?
Netrca: an effective network fault cause localization
Rpiplay implementation of raspberry pie airplay projector