PHP responsibility mode Singleton mode _ PHP Tutorial

Source: Internet
Author: User
PHP responsibility mode Singleton mode. Mode is very important for object-oriented development. One mode helps us create objects that can implement specific tasks and become class responsibilities. The mode also allows us to modify a class. the mode is very important for object-oriented development. One mode helps us create objects that can implement specific tasks and become class responsibilities. Mode also allows us to modify a class, but we do not need to modify the code related to this class. this is called the class polymorphism.

The Singleton mode is also called the responsibility mode. it is used to create a single function access point in the application. Next we will discuss and grasp the idea of Singleton in a down-to-earth manner and its application.

In complex systems, the use of Singleton mode is particularly useful in maintaining synchronization of application states. All Singleton classes have at least three elements:

  • A constructor marked as private.
  • Save the static member variables of the class instance.
  • The public static method used to access the instance.
Program List: Class of Singleton mode
 '; $ Test-> showColor (); // This will issue an E_USER_ERROR. // $ test_clone = clone $ test;?>

Program running result:

I am constructedMy color is !

We can see some special things from this program. Unlike a common class, a singleton class cannot be directly instantiated. it can only be instantiated by itself. To achieve this effect, the __construct () method must be marked as private. If you try to construct an object using a private constructor, an access-level error is returned.

How does a singleton class work? A singleton class provides an instance to other classes and uses it to call various methods. The singleton class returns a reference through an instance of internal storage, so the singleton class does not repeatedly occupy memory and system resources, so that other parts of the application can better use resources. Therefore, it is best to use the Singleton mode for your database access, so you won't create too many database connection instances to make your system run faster.

An empty _ clone () method is necessary to prevent objects from being copied or cloned.

Self: $ instance can detect whether the class has been initialized. If the static member of the saved instance is null or is not an instance of the class itself, the instance will be created and saved to the variable that stores the instance.

Program List: singleton without private constructor

There is no private constructor or reference for a non-strict Singleton. I don't know whether it is a mode.

  
  0 && $value < 100) self::$height = $value;  }  public function setWeight($value) {    if($value > 0 && $value < 100) self::$weight = $value;  }  public function __toString() {    return 'Fruit[height=' . self::$height . ', weight=' . self::$weight . ']';  }}// try to set data before any objects is createdFruit::$height = 55;$msm1 = Fruit::getInstance();  // use the getInstance() method$msm2 = new Fruit();           // use the default constructor$msm2->setWeight(78);                           // set data with an instantiated objectecho $msm1 . '
';echo $msm2 . '
';echo Fruit::getInstance() . '
';echo (new Fruit());?>

Program running result:

Fruit[height=55, weight=78]Fruit[height=55, weight=78]Fruit[height=55, weight=78]Fruit[height=55, weight=78]
Program List: database connection responsibilities
 _db = pg_connect('dbname=example_db');}private __clone() {};public static function getInstance() {if( ! (self::$_instance instanceof self) ){self::$_instance = new self();}return self::$_instance;}public function query($sql){return pg_query($this->_db,$sql);}}?>

How to use this singleton class?

$db = Database::getInstance();$db->query('SELECT * FROM example_table');

That is to say, there are some differences between the methods for getting objects and there is no special difference between them.

Bytes. One mode helps us create objects that can implement specific tasks and become class responsibilities. Mode also allows us to modify a class ,...

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.