1. The origin of the singleton pattern
Class
is an abstraction of a class of things with common characteristics in real life. Through the instantiation of a class, many objects are generated, but at the same time it also consumes a lot of resources (such as the limit on the number of connections when connecting to the database
, and for example, opening the resource manager on the computer
has uniqueness), which also resulted in the need to limit the instantiation of the class. In order to protect the uniqueness of resources, single case mode
was born.
2. Definition of singleton pattern
Definition: Singleton pattern is a design idea that a class design will only produce at most one object.
3. Instance of singleton mode
a. Create an empty class.
b. Being able to instantiate a class multiple times is the reason why multiple objects are generated, so you can privatize the constructor method.
<?php class Use{ } $a=new Use();//实例化一个对象 ?>c. Privatization
Constructor methodmakes the number of instantiated objects generated to 0, so that the constructor method can be called through
static method inside the class , and then return the constructor to the outside.
<?php class Use{ private function __construct() { echo __FUNCTION__."<br>"; } } ?>d. Although the object can be instantiated through the above method, it also releases the use rights of the constructor method. If you want this method to return only one object, you must ensure that there is a way to store a generated object inside the class. A new one is generated for the first time, and the old one is returned later. In this case, you need to use static properties.
<?php class Use{ private function __construct() { echo __FUNCTION__."<br>"; } public static function getInstance() { return new self(); } } $s1=Use::getSingleton(); ?>e. At this time, it can be guaranteed that only one
object will be obtained by calling the
static method. However, you can still instantiate new objects through
clone, so you can privatize
clone.
<?php class Use{ private static $object = NULL;//初始化为NULL,没有对象 private function __construct() { echo __FUNCTION__."<br>"; } public static function getInstance() { //判断类内部的静态属性是否存在对象 if(!(self::$object instanceof self)){ //当前保存的内容不是当前类的对象 self::$object = new self(); } //返回对象给外部 return self::$object; } } $s1=Use::getSingleton(); ?>
The above is the detailed content of Singleton pattern in php. For more information, please follow other related articles on the PHP Chinese website!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

JS 单例模式是一种常用的设计模式,它可以保证一个类只有一个实例。这种模式主要用于管理全局变量,避免命名冲突和重复加载,同时也可以减少内存占用,提高代码的可维护性和可扩展性。

去除方法:1、使用substr_replace()函数将首位数字替换为空字符串即可,语法“substr_replace($num,"",0,1)”;2、用substr截取从第二位数字开始的全部字符即可,语法“substr($num,1)”。

去掉方法:1、用substr_replace()删除右边n位置开始的全部字符,语法“substr_replace($str,"",-n)”,参数“n”为需要去除的字符个数;2、用substr(),语法“substr($str,0,-n)”。

php有操作时间的方法。php中提供了丰富的日期时间处理方法:1、date(),格式化本地日期和时间;2、mktime(),返回日期的时间戳;3、idate(),格式化本地时间为整数;4、strtotime(),将时间字符串转为时间戳等等。

php去掉数组键值的方法:1、使用“array_keys($array)”语句,可去掉全部键值,返回包含全部键名的数组;2、使用“array_splice($array,$start,$length)”语句,可去掉指定位置的一个或多个键值。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
