PHP design pattern singleton pattern

不言
Release: 2023-03-24 09:54:02
Original
1498 people have browsed it

The content of this article is about the singleton mode of PHP design pattern, which has certain reference value. Now I share it with you. Friends in need can refer to it

Single case mode ( Singleton)is also one of the creative modes, and it is not difficult to understand.
After reading some information, the most basic application is the task manager.
In fact, my personal understanding is thatcannot create a second identical object.

The program will internally determine whether the object has been created. If not, create it. If it already exists, use it directly.

Made some simple records, as follows:


        
Copy after login
hero = "诸葛亮"; echo "single1创建的英雄为:{$single1->hero}
"; // 第二次调用实例化对象变量方法 程序会判断出对象已经创建过,直接使用。 $single2 = Singleton::getInstance(); // 给hero赋值为 周瑜,会直接覆盖第一次的诸葛亮 $single2->hero = "周瑜"; echo "single1创建的英雄为:{$single1->hero}
"; echo "single2创建的英雄为:{$single2->hero}
";
Copy after login


##The output result is:

The hero created by single1 is: Zhuge Liang
The hero created by single1 is: Zhou Yu
The hero created by single2 is: Zhou Yu

Related recommendations :

Abstract Factory of PHP Design Pattern

Factory Method of PHP Design Pattern

PHP Design Pattern Simple Factory

The above is the detailed content of PHP design pattern singleton pattern. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!