PHP Singleton mode Simple implementation method, php example mode _php tutorial

Source: Internet
Author: User
Tags php example zend framework

PHP Singleton mode Simple implementation method, PHP example mode


A simple implementation method of PHP single-case mode

<?php  /** * design mode singleton mode * $_instance must be declared as a static private variable * Constructors and destructors must be declared private, preventing external programs from new * classes thereby losing the meaning of singleton patterns * getinstance () Method must be set to public, you must call this method * to return a reference to the instance *:: operator can only access static variables and static functions * new objects consume memory * Usage scenarios: The most common place is the database connection. * Once an object is generated using singleton mode, the object can be used by many other objects. */class Example {//Save example instance in this attribute private static $_instance;  The constructor is declared private, preventing direct creation of the object Private function __construct () {echo ' I am construceted ';}  Singleton method public static function singleton () {if (!isset (self::$_instance)) {$c =__class__; self::$_instance=new $c;} return Self::$_instance; }  //Prevents the user from replicating the object instance Public function __clone () {Trigger_error (' clone was 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 singleton 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 singleton pattern, so what is a singleton pattern?

The singleton pattern, as its name implies, is only one instance.

As the creation mode of an object, the singleton pattern ensures that a class has only one instance, and instantiates itself and provides this instance to the entire system.

This class is what we call a singleton class.

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.

Let's discuss why we use PHP singleton mode.

Most people understand its purpose from the literal meaning of the singleton pattern, and think that it is a kind of "family planning" that saves the system resources and avoids duplication of instantiation. Each time PHP executes the page, it cleans up all the resources from memory. Thus, in PHP, the single instance of the actual operation needs to be re-instantiated, thus losing the meaning of a single case of repeated instantiation. In this respect alone, PHP's singleton is indeed a bit disappointing for you. But does the singleton only have this function and application? The answer is no, let's take a look.

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

2. If a class is needed in the system to control some configuration information globally, it can be easily implemented using singleton mode. This can be see the Frontcontroller section of the Zend Framework.

3. In a page request, it is easy to debug, because all the code (such as database Operation class DB) is concentrated in a class, we can set the hooks in the class, output the log, so as to avoid var_dump everywhere, echo.

Above this PHP singleton mode simple Implementation method is small to share all the content of everyone, I hope to give you a reference, but also hope that we have a lot of support to help the home.

http://www.bkjia.com/PHPjc/1135012.html www.bkjia.com true http://www.bkjia.com/PHPjc/1135012.html techarticle PHP Singleton mode Simple implementation method, php example mode PHP singleton mode simple Implementation method PHP/** * design mode of a singleton mode * $_instance must be declared as static private variable * Construction ...

  • Related Article

    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.