The main points of the singleton pattern are three:
One is that a class can have only one instance;
The second is that it must create this instance on its own;
Thirdly, it must provide this instance to the whole system on its own.
Copy CodeThe code is as follows:
/* Example of a singleton pattern with the following key points:
*
*
1. $_instance must be declared as a static private variable
* 2. Constructors and clone functions must be declared private, in order to prevent the external program new class from losing the meaning of the singleton pattern
* 3. The getinstance () method must be declared as public, and this method must be called to return a reference to a unique instance
* 4. :: operator can only access static variables or static functions
* 5. PHP's singleton mode is relatively relative, because PHP's interpretation of the operating mechanism so that each PHP page is interpreted after execution, all the relevant resources will be recycled.
* That is, PHP has no way of keeping an object resident in memory at the language level. In PHP, all variables are page-level, regardless of global variables,
* or the static members of the class, will be emptied after the page executes, and the result will re-establish the new object, which will completely lose the meaning of Singleton.
* However, in the actual application of the same page may have multiple business logic, then the singleton mode plays a very important role, effectively avoid duplication
* New Object (note: The new object consumes memory resources) such a behavior, so we say that the PHP singleton mode is relatively
*
*/
class people
{
static private $_instance = NULL;
Public $height = ';
Public $age = ';
Private Function __construct ()
{
$this->height = ' 185 ';
$this->age = 25;
}
Private Function __clone ()
{
Do something
}
static public Function getinstance ()
{
if (!self::$_instance instanceof self)
{
Echo ' Lgh-big ';
Self::$_instance = new Self;
}
Else
{
For testing only
Echo ' Gdc-xiaoairener ';
}
return self::$_instance;
}
Public Function getheight ()
{
Echo $this->height;
}
Public Function Getage ()
{
Echo $this->age;
}
}
function Testinstance ()
{
People::getinstance ()->getage ();
}
Begin to use the class
$lgh = People::getinstance ();
$lgh->getheight ();
Echo '
';
Testinstance ();
?>
Advantages:Singleton mode avoids a large number of new operations, because each new operation consumes memory resources and system resources
Disadvantages:In PHP, all variables, whether global variables or static members of the class, are page-level, each time the page is executed, will re-establish a new object, will be emptied after the page executes, it seems that PHP singleton mode is meaningless, so PHP singleton mode I think only is significant when multiple scenarios occur for a single page-level request and need to share the same object resource.
why– Why should I use PHP singleton mode?
One of the main applications of PHP is application scenarios for dealing with databases, so there are a number of database operations in an application, such as the behavior of database handlers to connect to a database, and the use of Singleton mode to avoid a large number of new operations. Because each new operation consumes memory resources and system resources.
There is still some abstraction, given the code snippet.
Encode using traditional methods
Copy CodeThe code is as follows:
......
Initialize a database handle
$db = new db (...);
For example, there is an application scenario where you add a user information:
$db->adduserinfo ();
......
However, in another place we may want to find the user's information, this scenario appears in a function, this time to use the database handle resources, we may need to do so
......
function Test () {
......
At this point we have to reinitialize a database handle, imagine a number of scenarios, such code is how scary ah?!
$db = new db (...);
$db->getuserinfo ();
......
Some friends may say, I can not do this ah, I directly use the Global keyword is not OK? It is true that global solves the problem and acts as a singleton, but in OOP we refuse to write code because global has security implications, refer to the relevant books, and the singleton pattern is just an improvement on the global variables, Avoid global variables that store unique instances pollution namespaces
Global $db; In OOP, we don't advocate writing code like this
......
}
Using a singleton mode encoding
Copy CodeThe code is as follows:
......
All application scenarios have only one database handle resource, hey, the efficiency is old and high,
Resources are also greatly saved, the code is simple and clear:)
Db::getinstance ()->adduserinfo ();
Db::getinstance ()->getuserinfo ();
......
how– How to write PHP singleton mode?
After understanding the application scenario of the singleton pattern, we can learn the key points of PHP singleton pattern by writing the specific implementation code of the singleton pattern, the code is as follows:
Copy CodeThe code is as follows:
/**
* Examples of PHP singleton mode demo
* @author Guohua.li
* @modify 2010-07-11
* @website http://blog.163.com/lgh_2002/
*/
Class user{
/**
* Static finished variables save Global instance
* @access Private
*/
static private $_instance = NULL;
/**
* Privatization of constructors to prevent external instantiation of objects
*/
Private Function __construct () {}
/**
* Privatization of cloning functions to prevent external cloning of objects
*/
Private Function __clone () {}
/**
* static method, single access portal
* @return Object returns only instances of objects
*/
static public Function getinstance () {
if (Is_null (self::$_instance) | |!isset (self::$_instance)) {
Self::$_instance = new self ();
}
return self::$_instance;
}
/**
* Test method: Get user Name
*/
Public Function GetName () {
echo ' Hello liguohua! ';
}
}
From the above code, we summed up the PHP singleton mode implementation of the core points are as follows three:
1. A static member variable (usually $_instance private variable) that holds a unique instance of the class is required
2. Constructors and clone functions must be declared private, in order to prevent the external program new class from losing the meaning of the singleton pattern
3. A common static method that accesses this instance (usually the GetInstance method) must be provided to return a reference to a unique instance
Disadvantages of PHP singleton mode
As is known to all, the PHP language is an interpreted scripting language, which allows each PHP page to be interpreted and all related resources will be recycled. In other words, PHP does not have a language level to allow an object to reside in memory, which is different from the compilation of ASP. NET, Java, such as the single-instance in Java has been in the entire application life cycle, variables are cross-page level, It is true that this instance is unique in the application life cycle. However, in PHP, all variables, whether global variables or static members of the class, are page-level, each time the page is executed, the new object will be re-created, will be emptied after the page executes, so it seems that PHP singleton mode is meaningless, So PHP singleton mode I think it makes sense to just have multiple scenarios for a single page-level request and need to share the same object resource.
http://www.bkjia.com/PHPjc/327290.html www.bkjia.com true http://www.bkjia.com/PHPjc/327290.html techarticle The main points of the singleton pattern are three: one is that a class can have only one instance, and the other is that it must create this instance on its own, and thirdly, it must provide this instance to the whole system on its own. Copy generation ...