Skip to content

Instantly share code, notes, and snippets.

@rwellens
Created March 19, 2014 10:23
Show Gist options
  • Save rwellens/9639042 to your computer and use it in GitHub Desktop.
Save rwellens/9639042 to your computer and use it in GitHub Desktop.
<?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