Tag: His connection number static member handle connection specific static build related
First, what is a singleton mode?
1. Meaning
As an object's creation mode, Singleton mode ensures that a class has only one instance, and instantiates itself and provides this instance to the whole system globally. Instead of creating an instance copy, it returns a reference to the instance stored inside the singleton class.
2. Three points of the singleton mode:
(1) A static member variable that holds a unique instance of the class is required:
private static $obj;
(2) Constructors and clone functions must be declared private to prevent the external program new class from losing the meaning of the singleton pattern:
Private Function __construct ()
{
}
Private Function __clone ()
{
}//Overlay __clone () method, prohibit cloning about clone and __clone magic methods will be described in this blog
(3) A common static method that accesses this instance (usually the GetInstance method) must be provided to return a reference to a unique instance
public static function getinstance ()
{
if (!self:: $obj)
{
Self:: $obj = new self ();
}
return self::$_instance;
See here for classmates who don't understand self and $this.
Http://www.cnblogs.com/moxiaoan/p/6228905.html
The static variables and static methods do not understand that can be seen here.
Http://www.cnblogs.com/moxiaoan/p/6228948.html
Second, why should I use a singleton mode?
1. PHP Disadvantages:
The PHP language is an interpreted scripting language that allows every 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.
2, the single case mode in PHP application occasions:
(1) Application and database interaction
A large number of database operations exist in an application, such as a database handle to connect to a database, and a singleton pattern avoids a large number of new operations, because each new operation consumes memory resources and system resources.
(2) Control configuration information
If a class is needed in the system to control some configuration information globally, it can be easily implemented using singleton mode.
Third, how to achieve a single case mode?
1. Common Database Access Examples:
Initialize a database handle
$db = new db (...);
Add user Information
$db->adduserinfo (...);
......
Accessing the database in a function to find user information
function GetUserInfo ()
{
$db = new db (...); /again new database class, and database establish connection
$db = Query (...); /database access based on query statement
}
?>
2, apply the singleton mode to the database operation:
Class DB
{
private static $obj;
Private function __construct (...)
{
}
Private Function __clone () {};
public static function getinstance ()
{
if (! (Self:: $obj instanceof Self)) {//instanceof Use this keyword to determine whether an object is an instance of a class, a subclass of a class, or a specific interface is implemented.
Self:: $obj = new self ();
}
Return self:: $obj;
}
Public Function Adduserinfo (...)
{
}
Public function GetUserInfo (...)
{
}
}
Test
$db = Db::getinstance ();
$db->adduserinfo (...);
$db->getuserinfo (...);
?>
PHP Single-instance mode