Example of using a single example pattern for PHP design Patterns

Source: Internet
Author: User
Tags constructor php class

  Singleton mode is only instantiated once, which means that it is not possible to use the New keyword when instantiating, and here is the use of the example, you can refer to the use of the bar

The following is a single example pattern code: The code is as follows: <?php class easyframework_easy_mysql{    protected static $_instance = null;     Private Function __construct () {     }     public static function getinstance () {        if (self::$_instance = null) {            self::$_instance = new self (); &nb Sp      }         return self::$_instance;    }       protected function __clone () {     }  }   $x = Easyframewo Rk_easy_mysql::getinstance ();   Var_dump ($x);    ?>    / * 1. The first step:  * since it is a single case, that is, only once instantiated, it means that when instantiating  * it is not possible to use the NEW keyword!!!!  * the constructor in the class is invoked automatically when the new keyword is used.  * However, if we set the access control character of the constructor to protected or private  * then it is not possible to use the NEW keyword directly!!!  * The second step:  * regardless of the Protected/private modified properties or methods, in the current class  * can be accessed inside? ---> Can  * step three:  * now we have no way to get objects (because you can't use the new keyword),  * fourth: static members (including attributes or sideOn access, only through the  * class Name:: Property ()  * class Name:: Method ()  * step Fifth: If I now have a static method--> getinstance ()  * then it should be written as a call  * $object = Easyframework_easy_mysql::getinstance ()  * if the getinstance () method can get a unique object  * also represents the so-called single case pattern!!!  * sixth step, how to let Getinstace () only to one object?  * since to get the object, then certainly is:  * $variabl = new???? ();  * We also know that the values of static properties are inherited by all objects!!!  * static members belong to a class, not an Object!  * so:  * step seventh: Declare a static property with which to store the instantiated object  * protectd static $_instance  *  * and the initial value is null  * then I'm calling G Etinstance () method, you only need to determine whether its value is empty  *  * public static function getinstance () { *     if (self::_ instance = = null) { *      self::_instance = new self ();  *  }  *  return Self::_ Instance  *}  * in an instance, it must be written like this:  * $x = Easyframework_easy_mysql::getinstance ();  * on the first call, the $_instance value of the class is null, and  * then represents that the getinstance () method is true, and  * means  * self::$_ Instance this member has a value.!!!  * and also returns this value  * $y= Easyframework_easy_mysql::getinstance ();  * on the second or nth call, the self::$_instance already has a value  * also represents the GetInstance () method of the condition is FALSE!!!  * also represents that the program representatives could not perform the!!!  * also represents the return of the previous value directly!!!  *  *  *  * * *

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.