Home >Backend Development >PHP Tutorial >Let's talk about the singleton pattern in PHP
In the previous article "In-depth analysis of the template method pattern in PHP" we introduced the template method pattern in PHP. The following article will take you to understand the singleton in the PHP design pattern. model.
#The singleton pattern is definitely ranked first among commonly used and frequently asked interview design patterns. On the one hand, it is simple enough and can be explained in a few words. On the other hand, it is complex enough. Its implementation is not only in one form, but also needs to consider the issue of multi-thread locking in asynchronous languages such as Java. So during the interview, don't think that the interviewer will relax when he asks the question about the singleton model. This model can really be deep or shallow, and it can extremely reflect the level of a developer. Because as long as you work for a period of time, you will inevitably come into contact with this model.
GoF definition: Ensure that a class has only one instance and provide a global access point to access it.
GoF class diagram
##Code implementation
class Singleton { private static $uniqueInstance; private $singletonData = '单例类内部数据'; private function __construct() { // 构造方法私有化,外部不能直接实例化这个类 } public static function GetInstance() { if (self::$uniqueInstance == null) { self::$uniqueInstance = new Singleton(); } return self::$uniqueInstance; } public function SingletonOperation(){ $this->singletonData = '修改单例类内部数据'; } public function GetSigletonData() { return $this->singletonData; } }Yes, the core is such a singleton class, nothing else. Let the static variable save itself after instantiation. When this object is needed, call the GetInstance() method to obtain the globally unique object.
$singletonA = Singleton::GetInstance(); echo $singletonA->GetSigletonData(), PHP_EOL; $singletonB = Singleton::GetInstance(); if ($singletonA === $singletonB) { echo '相同的对象', PHP_EOL; } $singletonA->SingletonOperation(); // 这里修改的是A echo $singletonB->GetSigletonData(), PHP_EOL;When called by the client, we will find that
singletonB is exactly the same object.
The company is getting bigger and bigger, but we only have one copy (single instance class) of all company rosters, which is stored in our OA system. What I'm afraid of is that confusion will arise after each department has its own roster. For example, if the update is not timely, newly joined or resigned employees from other departments will be missed. Other departments can view the entire roster when needed, or create and modify their own department's sections based on the entire roster. But in the OA system, what they actually modify is the contents of the general roster. What everyone maintains is actually the only real roster stored in the OA system server
Full code: https://github.com/zhangyue0503/designpatterns-php/blob/master/21.singleton/source/singleton.php
Cache class diagram
##Full source code: https://github.com/ zhangyue0503/designpatterns-php/blob/master/21.singleton/source/singleton-http.phpDescription<?php class HttpService{ private static $instance; public function GetInstance(){ if(self::$instance == NULL){ self::$instance = new HttpService(); } return self::$instance; } public function Post(){ echo '发送Post请求', PHP_EOL; } public function Get(){ echo '发送Get请求', PHP_EOL; } } $httpA = new HttpService(); $httpA->Post(); $httpA->Get(); $httpB = new HttpService(); $httpB->Post(); $httpB->Get(); var_dump($httpA == $httpB);
In static languages such as Java , static variables can be directly new objects, in the declaration instance = new HttpService();. In this way, the GetInstance() method can be omitted, but this static variable will be directly instantiated and occupy memory regardless of whether it is used or not. This kind of singleton is called the hungry Chinese singleton pattern. Original address: https://juejin.cn/post/6844903990585458702 Author: Hardcore Project Manager Recommended learning: "PHP Video Tutorial"
The above is the detailed content of Let's talk about the singleton pattern in PHP. For more information, please follow other related articles on the PHP Chinese website!