Skip to content

Instantly share code, notes, and snippets.

@memclutter
Created May 3, 2015 06:57
Show Gist options
  • Select an option

  • Save memclutter/afa08ffe56365fd2b8c9 to your computer and use it in GitHub Desktop.

Select an option

Save memclutter/afa08ffe56365fd2b8c9 to your computer and use it in GitHub Desktop.
PHP Singleton Trait
<?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