当前位置:网站首页>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();
边栏推荐
- [use picgo+ Tencent cloud object to store cos as a map bed]
- Leetcode -- linked list
- 活动报名|Apache Pulsar x KubeSphere 在线 Meetup 火热报名中
- Directly applicable go coding specification
- [bug] @jsonformat has a problem that the date is less than one day when it is used
- 2021-05-20computed and watch applications and differences
- Data middle office: middle office practice and summary
- Squid proxy application
- Pytoch read data set (two modes: typical data set and user-defined data set)
- 2022-06-23: given a nonnegative array, select any number to make the maximum cumulative sum a multiple of 7, and return the maximum cumulative sum. N is larger, to the 5th power of 10. From meituan. 3
猜你喜欢

Every (), map (), forearch () methods. There are objects in the array

Netrca: an effective network fault cause localization

jupyter入门常见的几个坑:

12、 Demonstration of all function realization effects

Yolox backbone -- implementation of cspparknet

YOLOX backbone——CSPDarknet的实现

EasyExcel单sheet页与多sheet页写出

Groovy通过withCredentials获取Jenkins凭据

Kaformer personal notes

【LeetCode】387. First unique character in string
随机推荐
2022-06-23: given a nonnegative array, select any number to make the maximum cumulative sum a multiple of 7, and return the maximum cumulative sum. N is larger, to the 5th power of 10. From meituan. 3
MySQL - SQL statement
【ES6闯关】Promise堪比原生的自定义封装(万字)
Numpy NP in numpy c_ And np r_ Explain in detail
Squid proxy application
零基础自学SQL课程 | 相关子查询
Vidéo courte recommandée chaque semaine: Soyez sérieux en parlant de "métaunivers"
【LeetCode】541. Reverse string II
[ES6 breakthrough] promise is comparable to native custom encapsulation (10000 words)
How to import MDF and LDF files into MySQL workbench
Recommendation - Secret of curiosity: how many dancing angels can stand on the tip of a needle?
How to configure environment variables and distinguish environment packaging for multi terminal project of uniapp development
每周推荐短视频:谈论“元宇宙”要有严肃认真的态度
[noi Simulation Competition] send (tree DP)
[noi Simulation Competition] geiguo and time chicken (structure)
Cdga | how can we do well in data governance?
Numpy numpy中的np.c_和np.r_详解
Project deployment related
Yolox backbone -- implementation of cspparknet
Lu Qi: I am most optimistic about these four major technology trends