What is singleton pattern?
This design pattern allows only one object instance to be instantiated through this class in the entire application
Pattern classification?
In all pattern designs, there are three basic design patterns, singleton pattern, factory pattern, and registration tree pattern. Other patterns are often based on these patterns. Today we bring the singleton pattern.
Why use singleton pattern?
1. PHP often deals with databases. If connection objects are frequently established and new operations are performed in the application, a large amount of system memory resources will be consumed. (Save resource overhead)
2. In team cooperation projects, the singleton mode can effectively avoid artificial system consumption caused by different programmers new their own objects. (Save resource overhead)
------------------Achieving 3 steps + 1 song for a single instance ------------------
Step 1: Encapsulate the constructor method private __construct( ) { }
Reason: The constructor is the first method called when new creates an object. The constructor is declared as private or protected, which is destined to fail through new. Method creates instance objects.
Step 2: Create object instances through methods within the class. static Single(){ }
Reason: We often call the object's method after creating the object, and at this time we need to call the method in the class to create the object. The solution to a method that can be called regardless of whether the object is created or not is undoubtedly to use the keyword --static
Reason: Put the only instantiated object in this variable and store it
Step 4 (plus 1 song): Private The magic method of cloning: __c lo n e ();
Reason: For an object of a class, if you use the "clone operator", a new object that is exactly the same as the current object will be cloned, and: at this time this The new object will also automatically call the magic method in the class: _ _c l o n e (); as long as there is this method;
Code Demo
<?php class Sing { //第三步:定义一个变量 private static $instance= null; //第一步:封装构造函数 private function __construct(){ } //第二步:使用类名调用这个类创建对象实例 static function getSingle(){ if( !(self::$instance instanceof self) ){ //instanceof判断一个对象是否是某个类的实例 self::$instance = new self(); //用变量来存储实例化出来的对象 } return self::$instance; } //第四步:禁止克隆实例化出来的对象 private function __clone(){ } } $danli = Sing::getSingle(); var_dump($danli); //输出 object(Sing)#1 (0) { } $danli2 = Sing::getSingle(); var_dump($danli2); //输出 object(Sing)#1 (0) { } $obj3 = clone $danli; //此处禁止克隆单例对象实例 var_dump($obj3); //Call to private Sing::__clone() from context '' in 错误行号 ?>

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

SublimeText3 English version
Recommended: Win version, supports code prompts!

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

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
