Application Analysis in single-sample mode based on php design patterns _ PHP Tutorial

Source: Internet
Author: User
Application Analysis of the single sample mode based on the php design mode. Singleton mode: simply put, an object is only responsible for a specific task. Singleton class: 1. the constructor must be marked as private. the singleton class cannot be instantiated in other classes and can only be self Singleton mode: simply put, an object is only responsible for a specific task.

Singleton class:
1. the constructor must be marked as private. the singleton class cannot be instantiated in other classes and can only be instantiated by itself.
2. have a static member variable for the instance that saves the class
3. have a public static method to access this instance. [The getInstance () method is commonly used to instantiate a singleton class. the instanceof operator can detect whether the class has been instantiated]
Note: You need to create the _ clone () method to prevent objects from being copied.
Purpose:
1. php applications are mainly used for databases. Therefore, a large number of database operations exist in an application. Using the Singleton mode can avoid the resources consumed by a large number of new operations.
2. if you need a class in the system to globally control some configuration information, you can easily implement it using the Singleton mode. Refer to the ZF FrontController section.
3. summary of requests on a page for debugging. because all the code is concentrated in a class, we can set hooks in the class and output logs to avoid var_dump and echo everywhere.

The code is as follows:


Class DanLi {
// Static member variable
Private static $ _ instance;
// Private constructor
Private function _ construct (){
}
// Prevent the object from being cloned
Public function _ clone (){
Trigger_error ('Clone is not allow! ', E_USER_ERROR );
}
Public static function getInstance (){
If (! (Self: $ _ instance instanceof self )){
Self: $ _ instance = new self;
}
Return self: $ _ instance;
}
Public function test (){
Echo "OK ";
}
}

// Error: $ danli = new DanLi (); $ danli_clone = clone $ danli;
// Correct: $ danli = DanLi: getInstance (); $ danli-> test ();

?>

Bytes. Singleton class: 1. the constructor needs to mark it as private. the singleton class cannot be instantiated in other classes and can only be self-generated...

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.