_php example of the application of a simple example model of PHP with chicken ribs

Source: Internet
Author: User
Tags php language
The main points of a single case pattern are three:
One is that a class can have only one instance;
The second is that it must create this instance by itself;
Third, it must provide this example to the whole system.
Copy Code code as follows:

<?php
/* Single example mode, the main points are as follows:
*
* 1. $_instance must be declared as a static private variable
* 2. Constructors and cloning functions must be declared private, in order to prevent the external program new class from losing the meaning of a single case 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. The single example mode of PHP is relative, because the PHP explanation runs so that every PHP page is interpreted and all the related resources are recycled.
* That is, PHP has no way to keep an object resident in the language level. In PHP, all variables are page-level, regardless of global variables,
* or static members of the class, will be emptied after the completion of the page, the result will be to re-establish new objects, so that completely lost the meaning of Singleton.
* However, there may be multiple business logic in the same page in the actual application, when the single case pattern plays a very important role, effectively avoiding duplication
* New Object (note: The new object consumes memory resources) so one behavior, so we say that PHP's single case pattern 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
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 ' <br/> ';
Testinstance ();
?>

Advantages:A single case pattern avoids a large number of new operations, because each new operation consumes memory resources and system resources
Disadvantages:In PHP, all variables are global variables or static members of a class, are page-level, every time the page is executed, will re-establish new objects, will be emptied after the completion of the page, so it seems that the PHP single example mode has no meaning, so the PHP single example mode I think only It makes sense to have multiple scenarios when a single page-level request occurs and to share the same object resource.

why– Why use PHP as a single example?
One of the main applications of PHP is the application scene with the database, so a large number of database operations in an application, such as a database handle to connect to the database, the use of a single example mode to avoid a large number of new operations, Because each new operation consumes memory resources and system resources.
Or some abstraction, give a snippet of code.
Coding using traditional methods
Copy Code code as follows:

<?php
......
Initialization of a database handle
$db = new db (...);
For example, an application scenario is to add a user information:
$db->adduserinfo ();
......
However, we may have to look up the user's information in another place, and this scenario appears in a function that uses a database handle resource, which we might need to do.
......
function Test () {
......
At this time we have to reinitialize a database handle, imagine how many scenarios, such a code is terrible AH?!
$db = new db (...);
$db->getuserinfo ();
......
Some friends may say, I also can not do so, I directly use the Global keyword is not OK? Indeed, global can solve the problem, but also play the role of a single example, but in OOP, we refuse to write code, because the global has a security problem, please refer to the relevant books, while the single case pattern is just an improvement of global variables, Avoid global variable pollution namespaces that store unique instances
Global $db; In OOP, we don't advocate writing code like this
......
}

Using a single case pattern encoding
Copy Code code as follows:

<?php
......
All the application scenarios have only one database handle resource, hey, the efficiency is old and high,
Resources are also greatly saved, the code is concise and clear:)
Db::getinstance ()->adduserinfo ();
Db::getinstance ()->getuserinfo ();
......

how– How do I write PHP single example mode?
After understanding the application scenario of the single case pattern, we will grasp the core points of the PHP single example pattern by writing the code of the specific implementation of the single example pattern, as follows:
Copy Code code as follows:

<?php
/**
* Example of PHP single example mode demo
* @author Guohua.li
* @modify 2010-07-11
* @website http://blog.163.com/lgh_2002/
*/
Class user{
/**
* Static production 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 the external cloning of objects
*/
Private Function __clone () {}
/**
* static method, single case unified Access Portal
* @return Object returns a unique instance of the 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 summarized the PHP single example mode implementation of the core points are as follows three:
1. A static member variable (usually a $_instance private variable) that holds a unique instance of the class is required.
2. Constructors and cloning functions must be declared private, in order to prevent the external program new class from losing the meaning of a single case pattern
3. A public static method (usually the GetInstance method) that accesses this instance must be provided to return a reference to a unique instance
Disadvantages of the PHP single example pattern
As we all know, the PHP language is an interpreted scripting language, which enables each PHP page to be interpreted and executed, and all related resources are recycled. In other words, PHP does not have the language level to make an object resident memory, which is different from ASP.net, Java, such as a single meeting in Java throughout the lifecycle of the application, variables are across the page level, The uniqueness of this instance in the life cycle of the application is truly achievable. 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 created, will be emptied after the completion of the page, so it seems that the PHP single example mode has no meaning, So PHP Single example mode I think it makes sense to only have multiple scenarios when it comes to a single page-level request and need to share the same object resource.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.