PHP Design Pattern single case mode Use example _php instance

Source: Internet
Author: User

The following is a single example pattern code:

Copy Code code 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 ();
}
return self::$_instance;
}

protected function __clone () {

}

}

$x = Easyframework_easy_mysql::getinstance ();

Var_dump ($x);


?>


/*
* 1. First step:
* Since it is a singleton, that is, it can only be instantiated once, which means that when instantiated
* It is impossible 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's impossible to use the NEW keyword directly!!!
* Second Step:
* Regardless of protected/private modified properties or methods, please ask in the current class of
* Is there any access to the inside? ---> Can
* Step Three:
* Now we have no way to get the object (because you can't use the new keyword),
* Step Fourth: Static members (including properties or methods) can only be accessed through the
* Class Name:: Properties ()
* Class Name:: Method ()
* Fifth Step: If I now exist a static method--> getinstance ()
* Then the call should be written as
* $object = Easyframework_easy_mysql::getinstance ()
* If the getinstance () method can get a unique object
* Also represents the so-called single case mode of!!!
* Sixth step, how to let Getinstace () only to one object?
* If you want to get the object, then it must be:
* $variabl = new???? ();
* We also know that the value of a static property is a!!! that can be inherited by all objects.
* Static members belong to the class, not the 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 when I call the getinstance () method, I just need to determine if the value is empty
*
* public static function getinstance () {
* if (self::_instance = = null) {
* Self::_instance = new self ();
*  }
* Return self::_instance;
* }
* In instances, it must be written like this:
* $x = Easyframework_easy_mysql::getinstance ();
* At the first call, the $_instance of the class is null, and the value of the static property
* So it also means that the getinstance () method of judgment is true,
* And it means
* Self::$_instance This member has a value!!!
* and also returns this value
* $y = Easyframework_easy_mysql::getinstance ();
* In the second or Nth call, the self::$_instance already has a value
* Also on behalf of getinstance () The condition of the method is FALSE!!!
* It also means that the program representatives are unlikely to perform the!!!
* Also means that will return directly to the previous value of the!!!
*
*
*
* */

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.