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) |