PHP design pattern example singleton mode_PHP tutorial
Jul 13, 2016 pm 05:15 PMThis article will introduce you to the singleton mode method of PHP design pattern examples. Friends who need to know more can enter it for reference.
Single instance class:
1. The constructor needs to be marked as private. A singleton class cannot be instantiated in other classes, but can only be instantiated by itself
2. Have an instance static member variable that saves the class
3. Have a public static method to access this instance. [The getInstance() method is commonly used to instantiate singleton classes, and the instanceof operator can be used to detect whether this class has been instantiated]
Note: You need to create a __clone() method to prevent objects from being copied
Function:
1.php applications are mainly used for databases, so there will be a large number of database operations in an application. Using the singleton mode can avoid the resources consumed by a large number of new operations
2. If a class is needed in the system to globally control certain configuration information, it can be easily implemented using the singleton mode. Refer to the FrontController part of ZF
3. Request summary on one page to facilitate debugging, because all the code is concentrated in one class, we can set hooks in the class and output logs, thereby avoiding var_dump and echo everywhere.
Example of PHP singleton pattern.
The code is as follows | Copy code | ||||
|
Let’s discuss why we should use PHP singleton mode?
Most people understand its purpose from the literal meaning of the singleton pattern, thinking that it saves system resources, avoids repeated instantiation, and is a kind of "family planning". And PHP executes it every time All resources will be cleared from memory when the page is completed. Therefore, the singleton in PHP actually needs to be re-instantiated every time it is run, thus losing the meaning of repeated instantiation of the singleton. From this aspect alone It is said that PHP's singleton is indeed a bit disappointing to everyone. But does a singleton only have this function and application? The answer is no, let's take a look.
1. PHP is mainly used in database applications, so there will be a large number of database operations in an application. When developing in an object-oriented way (nonsense), if you use the singleton mode, you can avoid a lot of new operation consumption. resources.
2. If a class is needed to globally control certain configuration information in the system, it can be easily implemented using the singleton mode. This can be found in the FrontController part of zend Framework.
3. In a page request, it is easy to debug because all the code (such as database operation class db) is concentrated in one class. We can set hooks in the class and output logs to avoid var_dump and echo everywhere. .
The code is as follows
|
Copy code | ||||
/**
{ //Save the example instance in this attribute
Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

How To Set Up Visual Studio Code (VS Code) for PHP Development
