PHP single-case mode (duty mode)

Source: Internet
Author: User
Simply put, an object (before learning design patterns, need to understand the object-oriented thinking) is only responsible for a specific task;

Singleton class:

1. The constructor needs to be marked as private (access control: Prevent external code from creating objects using the new operator), the Singleton class cannot be instantiated in other classes, and can only be instantiated by itself;

2. Have a static member variable that holds an instance of the class

3, has a public access to this instance of the static method (common getinstance () method to instantiate a singleton class, through the instanceof operator can detect whether the class has been instantiated)

In addition, you need to create a __clone () method to prevent the object from being copied (cloned)

<?php/** * Created by Phpstorm. * User: * date:2017/1/2 * Time: PM 3:32 */class Single {    ///save private static instance variable for instance private    $instance;    The private tag is constructed in a way that instantiates the    private function __construct () {        echo "This is construct function")    by other means;    The Clone method is created to prevent the cloned public    function __clone () {        trigger_error ("clone was not allowed", e_user_error);    }    A singleton method, a public static method that the user accesses to an instance, publicly    static function shareinstance () {        //instanceof: Determines whether the instance is an instance of self or a subclass        if (! ( Self:: $instance instanceof self) {self            :: $instance = new self ();        }        Return self:: $instance;    }}

Report

trigger_error()The function creates a user-level error message.

trigger_error()The function can be associated with a built-in error handler, or you can use a user-defined function as a new error handler (Set_error_handler ()).

Trigger_error (Errormsg,errortype);
Parameters Description
ErrorMsg Necessary. Specifies the error message. The maximum length is 1024 bytes.
ErrorType Optional. Specifies the type of error. Possible values: E_user_errore_user_warninge_user_notice (default)


Simply put, an object (before learning design patterns, need to understand the object-oriented thinking) is only responsible for a specific task;

Singleton class:

1. The constructor needs to be marked as private (access control: Prevent external code from creating objects using the new operator), the Singleton class cannot be instantiated in other classes, and can only be instantiated by itself;

2. Have a static member variable that holds an instance of the class

3, has a public access to this instance of the static method (common getinstance () method to instantiate a singleton class, through the instanceof operator can detect whether the class has been instantiated)

In addition, you need to create a __clone () method to prevent the object from being copied (cloned)

<?php/** * Created by Phpstorm. * User: * date:2017/1/2 * Time: PM 3:32 */class Single {    ///save private static instance variable for instance private    $instance;    The private tag is constructed in a way that instantiates the    private function __construct () {        echo "This is construct function")    by other means;    The Clone method is created to prevent the cloned public    function __clone () {        trigger_error ("clone was not allowed", e_user_error);    }    A singleton method, a public static method that the user accesses to an instance, publicly    static function shareinstance () {        //instanceof: Determines whether the instance is an instance of self or a subclass        if (! ( Self:: $instance instanceof self) {self            :: $instance = new self ();        }        Return self:: $instance;    }}

Report

trigger_error()The function creates a user-level error message.

trigger_error()The function can be associated with a built-in error handler, or you can use a user-defined function as a new error handler (Set_error_handler ()).

Trigger_error (Errormsg,errortype);
Parameters Description
ErrorMsg Necessary. Specifies the error message. The maximum length is 1024 bytes.
ErrorType Optional. Specifies the type of error. Possible values: E_user_errore_user_warninge_user_notice (default)

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.