PHP design mode single-instance mode

Source: Internet
Author: User

First clarify the singleton pattern: Singleton mode is the best solution if you want to have only one object for a class in the system.

Next, let's refine some of the key points of a singleton pattern: if there is a singleton class called Singleton, then:

1 Singletonobj objects should be able to be used by any object in the system

2 Singletonobj objects should not be stored in a global variable that will be overwritten

3 There should be no more than one Singletonobj object in the system, that is, a object can set a property of the Singletonobj object, and the B object can get the value of the property directly without having to pass any other object.

In order to solve this problem, we start with the problem itself, that is, how to control the instantiation of a class, so that the instantiation of the class can only be instantiated once, this may sound really difficult, but in fact very simple, only need to define a private construction method:


Next look at the code:

<?php/** * Created by Phpstorm.    * User:evolution * date:14-12-7 * Time: PM 2:40 */class Singleton {//single object properties private static $instance;    Define some global variables that need to hold the property private $props = Array ();    Private constructor Method __construct () {echo ' into construct! ';            }//Returns a singleton public static function getinstance () {//To determine if an instantiated object already exists if (Empty (self:: $instance)) {            Can be override (dynamic parsing) Self:: $instance = new static ();        Cannot be override (static parsing)//self:: $instance = new self ();    } return Self:: $instance;    }//Set property public function SetProperty ($key, $value) {$this->props[$key] = $value;    }//Get property Public Function Getpeoperty ($key) {return $this->props[$key]; }}//http://stackoverflow.com/questions/5197300/new-self-vs-new-static//http://www.jb51.net/article/54167.htm$ Singleobj = Singleton::getinstance (); $singleObj->setproperty (' Root_path ', '/www '); $SINGLEOBJ->setpropeRty (' Tmp_path ', '/tmp ');//The next time you delete the Singleton object, if you can also get the property you just added, you are using the same object unset ($SINGLEOBJ); $singleObj = Singleton:: GetInstance (); Echo $singleObj->getpeoperty (' Root_path '); Echo $singleObj->getpeoperty (' Tmp_path ');

The differences between new static () and new self () can be explained by the following:

Http://stackoverflow.com/questions/5197300/new-self-vs-new-static

Http://www.jb51.net/article/54167.htm

If there is something wrong, welcome everyone, together to discuss!




PHP design mode single-instance mode

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.