A little doubt about the singleton object
static function instance($type = 'op') { $class = 'blog'; if (!isset(self::$_instance)) { $_tmp = new $class(); self::$_instance = $_tmp; $tmp = null; } return self::$_instance; }
The same page continuously requests this Singleton object by refreshing the page. Do you want to recreate a singleton instance without refreshing it once?
Can I create different strengths based on the input TYPE. How to write isset (self ::$ _ instance)
Reply to discussion (solution)
Singleton is only a singleton in the current program
Refreshing the page is to start a new program.
The TYPE passed in on the refresh page must not be found in self ::$ _ instance?
Open two webpages to access this page and create two singleton objects. what is the significance of this process for singleton instances? Please try again
In the code you posted, the parameter $ type is not displayed.
So we cannot discuss the topic related to this parameter.
A Singleton maintains an instance (no matter how many times it is instantiated) during the entire program running)
A singleton is generally used to transmit data between objects, thus separating algorithms from data.
Once the page is refreshed, the code is run from the beginning.
When the server receives a connection request, a thread is generated. this thread calls php (or other languages), transmits the connection to the client, disconnects the connection, and ends the thread.
Refresh is a connection request process, and is naturally a new thread.
Because
1. php wireless communication between threads, each connection thread is independent, so even if the same program/script is run separately
2. most importantly, the above threads are actually apache (or other servers) threads, which have nothing to do with php. for example, it is equivalent to opening N browsers, you can view the same webpage, but neither of them can obtain the same content.
A singleton ensures that a class has only one instance. the implementation method is to first determine whether the instance exists or not. if the instance exists, it is returned directly. if the instance does not exist, it is created and then returned, this ensures that a class has only one instance object.
Generally, you can replace all the class members with static ones.
PHP is an interpreted scripting language. this running mechanism allows all related resources to be recycled after each PHP page is interpreted and executed. That is to say, PHP cannot make an object resident in the memory at the language level, which is different from asp.net, Java, and other compilation types, for example, in Java, the single meeting always exists throughout the application lifecycle, and variables are cross-page-level, so that this instance can truly be unique in the application lifecycle. However, in PHP, all variables, whether global variables or static members of the class, are page-level. every time a page is executed, a new object will be created, will be cleared after the page is executed, so it seems that the PHP Singleton mode is meaningless, therefore, the PHP Singleton mode makes sense only when multiple application scenarios occur during a single page-level request and the same object resource needs to be shared.
The solution is very simple ,.. I think it hurts myself .. Two different singleton instances are directly returned ..
I understand and often use the Singleton mode. It doesn't quite understand where the benefits it brings to the program are .. Maybe my development projects are still relatively small. At best, 100WPV is average daily.