1. the constructor must be marked as private. the singleton class cannot be instantiated in other classes and can only be instantiated by itself.
2. have a static member variable for the instance that saves the class
3. have a public static method to access this instance. [The getInstance () method is commonly used to instantiate a singleton class. the instanceof operator can detect whether the class has been instantiated]
Note: You need to create the _ clone () method to prevent objects from being copied.
1. php applications are mainly used for databases. Therefore, a large number of database operations exist in an application. Using the Singleton mode can avoid the resources consumed by a large number of new operations.
2. if you need a class in the system to globally control some configuration information, you can easily implement it using the Singleton mode. Refer to the ZF FrontController section.
3. summary of requests on a page for debugging. because all the code is concentrated in a class, we can set hooks in the class and output logs to avoid var_dump and echo everywhere.
Example of the php Singleton mode.
|The code is as follows:|
Next we will discuss why we should use the PHP Singleton mode?
Most people understand the purpose of the Singleton model literally. they think that this is a saving of system resources and can avoid repeated instantiation. it is a kind of "family planning ". PHP clears all resources from the memory after each execution of the page. therefore, in PHP, the singleton needs to be re-instantiated every time it is run, thus losing the significance of the Singleton repeated instantiation. in this regard, the PHP Singleton is indeed a bit disappointing. but does the Singleton only have this function and application? The answer is No. let's take a look.
1. php applications mainly involve database applications. Therefore, a large number of database operations exist in an application. when using the object-oriented method for development (nonsense), if you use the Singleton mode, this avoids the resource consumption of a large number of new operations.
2. if you need a class in the system to globally control some configuration information, you can easily implement it using the Singleton mode. for details, refer to the FrontController section of zend Framework.
3. in a page request, debugging is easy, because all the code (such as database operation db) is concentrated in one class, we can set hooks in the class and output logs, this avoids var_dump and echo everywhere.
|The code is as follows:|
// The constructor declares private to prevent direct object creation.
// Singleton method
// Prevents users from copying object instances
Function test ()
// This write method will fail because the constructor is declared as private.
// The following is a singleton object of the Example class.
// Copying an object will result in an E_USER_ERROR.
Bytes. Singleton class: 1. the constructor must be marked as private. the singleton class is not...