Created
March 30, 2018 07:15
-
-
Save wiyoe/9ecee5213e3a73b745fa13b1ec611d23 to your computer and use it in GitHub Desktop.
PHP singleton
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Manager | |
| { | |
| private static $instance; | |
| public static function get_instance() | |
| { | |
| if (!isset(self::$instance)) { | |
| $class_name = __CLASS__; | |
| self::$instance = new $class_name; | |
| } | |
| return self::$instance; | |
| } | |
| public function hi() | |
| { | |
| echo 'hi'; | |
| } | |
| public function __clone() | |
| { | |
| trigger_error('Clone is not allowed for singleton object.', E_USER_ERROR); | |
| } | |
| } | |
| Manager::get_instance()->hi(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment