Created
May 3, 2015 06:57
-
-
Save memclutter/afa08ffe56365fd2b8c9 to your computer and use it in GitHub Desktop.
PHP Singleton Trait
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
| <?php | |
| /** | |
| * Singleton Trait. | |
| * | |
| * Example: | |
| * | |
| * class MyClass | |
| * { | |
| * use SingletonTrait; | |
| * } | |
| * | |
| * $my = MyClass::getInstance(); | |
| */ | |
| trait SingletonTrait | |
| { | |
| private static $_instance; | |
| public static function getInstance() | |
| { | |
| if (!$this->_instance) { | |
| $this->_instance = new static(); | |
| } | |
| return $this->_instance; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment