Created
March 19, 2014 10:23
-
-
Save rwellens/9639042 to your computer and use it in GitHub Desktop.
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 | |
| /** | |
| * SingletonTrait.php | |
| * | |
| * @date 28/02/2014 | |
| * @file SingletonTrait.php | |
| */ | |
| trait SingletonTrait | |
| { | |
| /** | |
| * protected | |
| */ | |
| protected function __construct(){ | |
| if(method_exists($this, 'init')) { | |
| $this->init(); | |
| } | |
| } | |
| /** | |
| * @var static | |
| */ | |
| protected static $instance; | |
| /** | |
| * @return static | |
| */ | |
| public static function getInstance() | |
| { | |
| if(!static::$instance) { | |
| static::$instance = new static; | |
| } | |
| return static::$instance; | |
| } | |
| public function __clone() | |
| { | |
| throw new Exception('Object follows singleton pattern and cannot be cloned'); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment