Skip to content

Instantly share code, notes, and snippets.

@bayi
Created February 10, 2013 01:16
Show Gist options
  • Save bayi/4747873 to your computer and use it in GitHub Desktop.
Save bayi/4747873 to your computer and use it in GitHub Desktop.
Singleton Trait for PHP
<?php
trait SingletonClass {
protected static $instance;
final public static function getInstance() {
return isset(static::$instance)
? static::$instance
: static::$instance = new static;
}
final private function __construct() {
$this->__init();
}
protected abstract function __init();
final private function __wakeup() {}
final private function __clone() {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment