English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Concept of singleton pattern
The singleton pattern refers to the design pattern that only one object instance of the class exists in the entire application.
Characteristics of the singleton pattern
A class has only one instance throughout the application
The class must create this instance itself
It must provide this instance to the entire system itself
Reasons for using the singleton pattern in PHP
I use PHP to interact with various databases most of the time, including MySQL, Redis, Memcache, and various relational and non-relational databases, so there will be a large number of operations to connect to the database in an application. If the singleton pattern is not used, you will have to perform a new operation each time, but each new operation will consume a large amount of memory resources and system resources, and each opening and closing of a database connection is a great test and waste for the database.
A static member variable is needed to store the unique instance of the class (usually $instance is a private variable)
The constructor and clone function must be declared as private to prevent external programs from creating new instances of the class, thus losing the meaning of the singleton pattern
It must provide a public static method to access this instance, thus returning a reference to the unique instance
//Design pattern //Singleton pattern uses the same object //class Ren //{ // public $name; // private static $dx; //Store the object // // private function __construct() // { // // } // // public static function DuiXiang() // { // if(empty(self::$dx)) // { // self:;$dx = new Ren(); // } // return self::$dx; // } //} // //$r = Ren::DuiXiang(); //Changed the constructor to private //Created a method to indirectly create objects //Add restrictions in this method
Factory class
It refers to a class that contains a method specifically used to create other objects, the factory class is crucial in the practice of polymorphism programming, it allows dynamic replacement of classes, modification of configurations, and usually makes the application more flexible. It is very important for advanced PHP developers to master the advanced factory pattern.
The factory pattern is usually used to return different classes that conform to similar interfaces. A common usage of the factory is to create polymorphic providers, thereby allowing us to decide which class to instantiate based on application logic or configuration settings. For example, such providers can be used to extend a class without needing to refactor other parts of the application, thus using the new extended name.
Generally, the factory pattern has a key constructor, named according to general principles as the static method 'Factory', however, this is only a principle, the factory method can be arbitrarily named, and this static method can also accept parameters of arbitrary data types, and must return an object.
class YunSuan { public $a; public $b; function Suan() { echo "Perform arithmetic operations on two numbers"; } class gongchang { //Function: Given a parameter, return an object static function chanpin($name) { switch($name) { case "'+: return new jia(); break; case "-"; return new jian(); break; } } } $a=gongchang::chanpin("+");
That's all for this article. I hope the content of this article can bring some help to everyone's learning or work, and I also hope to get more support for the Shoutaotutorial!
Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email for reporting. Provide relevant evidence, and once verified, this site will immediately delete the infringing content.)