A simple implementation method of PHP Single example mode _php example

Source: Internet
Author: User
Tags instance method zend framework

Simple implementation for PHP single mode

<?php 
 
/** 
* design mode 
* $_instance must be declared as a static private variable 
* Constructors and destructors must be declared private to prevent the external program new 
* class from losing the meaning of a single case pattern 
* The getinstance () method must be set to public, and this method must be called 
to return a reference to the instance 
*:: operator can only access static and static functions 
* New objects consume memory 
* Usage scenarios: The most common place is the database connection. 
* After an object is generated using a single case pattern, 
the object can be used by many other objects. 
*
/class Example 
{ 
//Save example instance in this property 
private static $_instance; 
 
Constructor declared private to prevent direct creation of object 
Private Function __construct () 
{ 
echo ' I am construceted '; 
} 
 
Single-instance method public 
static function singleton () 
{ 
if (!isset (self::$_instance)) 
{ 
$c =__class__; 
Self::$_instance=new $c; 
} 
return self::$_instance; 
} 
 
To prevent users from replicating object instances public 
function __clone () 
{ 
trigger_error (' Clone are not allow ', e_user_error); 
} 
 
function test () 
{ 
echo ("test"); 
 
} 
 
This is an error, because the construction method is declared private 
$test = new Example; 
 
The following will get the single example object of the Example class 
$test = Example::singleton (); 
$test->test (); 
 
Copying an object will result in a e_user_error. 
$test _clone = Clone $test; 
? >

First we need to know the concept of a single case pattern, so what is a single case pattern?

Singleton mode, as the name suggests, is only one instance.

As an object's creation pattern, the singleton pattern ensures that a class has only one instance, and instantiates it and supplies the instance to the entire system.

We call this Class A single instance class.

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.

Let's discuss why we should use the PHP single example mode.

Most people understand its purpose from the literal meaning of the singleton pattern, which is a kind of "family planning", which is to save the resources of the system, to avoid the repetition of the instantiation. And every time PHP finishes the page, it clears all the resources out of memory. As a result, the single example of PHP in the actual operation of each run is to be instantiated, so that the single example of repeated instantiation of the meaning of. In this respect alone, PHP's single example is a bit disappointing to everyone. But is the single example just this function and application? The answer is no, let's look at it together.

1. PHP application is mainly in the database application, so an application will have a large number of database operations, in the use of object-oriented mode of development (nonsense), if the use of single case mode, you can avoid a large number of new operations to consume resources.

2. If you need a class in the system to control some configuration information globally, it is convenient to use a single example pattern. This can be referred to the Frontcontroller section of the Zend Framework.

3. In a page request, easy to debug, because all the code (such as Database Operations class DB) are concentrated in a class, we can set the hook in the class, output log, so as to avoid everywhere var_dump, echo.

The above PHP single example mode of simple implementation is a small series to share all the content, I hope to give you a reference, but also hope that we support the cloud habitat community.

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.