This article analyzes the singleton pattern of PHP design pattern in examples. Share it for your reference, as follows:
Singleton Pattern (Singleton Pattern single-piece mode or single-element mode) is a common design pattern, which has three characteristics.
- 1. There can only be one instance
- 2. This instance must be created by yourself
- 3. This instance must be provided to other objects
Here is the PHP code to implement it
<?PHP /** * Created by PHPStorm. * User: tiansi * Date: 18/1/2 * Time: 3:40 pm */ class Signleton{ private static $_instanse = null; //Private construction method to prevent the outside world from using new instantiation objects private function __construct() { } //Private cloning method to prevent external cloning of objects private function __clone() { // TODO: Implement __clone() method. } //Static provides singleton access portal static function getInstance(){ if (is_null(self::$_instanse) || !isset(self::$_instanse)){ self::$_instanse = new self(); } return self::$_instanse; } public function say(){ echo 'I am signleton'; } }
Let's try calling below
<?PHP /** * Created by PHPStorm. * User: tiansi * Date: 18/1/2 * Time: 3:48 pm */ //The first method will report an error because the construction method is privatized//PHP Fatal error: Uncaught Error: Call to private Signleton::__construct() from invalid context in /Users/apple/uxin/:11 /* $signleton1 = new Signleton(); $signleton1->say(); */ //The above code error: Fatal error: Call to private Signleton::__construct()//Instantiation is successful and output I am singleton$signleton2 = Signleton::getInstance(); $signleton2->say();
Running results:
I am signleton
Its Pros and Cons
advantage:
- 1. In the singleton pattern, there is only one instance of the active singleton, and all instantiation of the singleton class is obtained with the same instance. This prevents other objects from instantiating themselves, ensuring that all objects access an instance
- 2. The singleton pattern has a certain scalability. The class controls the instantiation process by itself. The class has corresponding scalability when changing the instantiation process.
- 3. Provides controlled access to the unique instance.
- 4. Since there is only one object in the system memory, system resources can be saved. Singleton mode can undoubtedly improve system performance when objects that need to be frequently created and destroyed.
- 5. Allow variable number of instances.
- 6. Avoid multiple occupations of shared resources.
shortcoming:
- 1. Not suitable for changing objects. If objects of the same type always change in different use case scenarios, singletons will cause data errors and cannot save each other's states.
- 2. Since there is no abstraction layer in the single interest pattern, it is very difficult to expand the singleton class.
- 3. The responsibilities of single-case categories are too heavy, which to a certain extent violates the "principle of single-service".
- 4. Abuse of singletons will bring some negative problems. For example, designing database connection pool objects as singleton classes to save resources may lead to too many programs sharing connection pool objects and causing connection pool overflow; if the instantiated object is not used for a long time, the system will consider it to be garbage and recycled, which will lead to the loss of object state.
This is the advantage and disadvantage of singleton pattern, because the language types are different, PHP's singleton pattern is different.
As we all know, PHP language is an interpreted scripting language. This operating mechanism allows each PHP page to be interpreted and executed, all relevant resources will be recycled. In other words, PHP has no way to make an object resident in memory at the language level, which is different from compilation types such as Java. For example, in Java, singletons will always exist in the entire application life cycle, and variables are cross-page level, which can truly achieve the uniqueness of this instance in the application life cycle. However, in PHP, all variables, whether global variables or static members of the class, are page-level. Every time the page is executed, a new object will be re-created and will be cleared after the page is executed. This seems that PHP singleton mode has no meaning, so I think it is useful only when multiple application scenarios appear in a single page-level request and the same object resource needs to be shared.
for example
1. Application and database interaction
There will be a large number of database operations in an application, such as connecting to the database through a database handle. Using a singleton pattern can avoid a large number ofnew
Operation, because every new operation consumes memory resources and system resources.
2. Control configuration information
If there is a class in the system that needs to control certain configuration information globally, then using singleton mode can be easily implemented
For more information about PHP related content, please check out the topic of this site:PHP object-oriented programming tutorial》、《Complete collection of PHP array (Array) operation techniques》、《Introduction to PHP basic syntax》、《Summary of PHP operations and operator usage》、《Summary of usage of php strings》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php》
I hope this article will be helpful to everyone's PHP programming.