Skip to content

Instantly share code, notes, and snippets.

@shadda
Created May 24, 2014 03:25
Show Gist options
  • Select an option

  • Save shadda/39e34f7c8594fe3b52de to your computer and use it in GitHub Desktop.

Select an option

Save shadda/39e34f7c8594fe3b52de to your computer and use it in GitHub Desktop.
<?php
trait Singleton
{
private static $_instance;
public static function getInstance()
{
if(!(self::$_instance instanceof self))
{
self::$_instance = new self;
}
return self::$_instance;
}
}
//and in another class
class SomeShit
{
use Singleton;
}
$singleton = SomeShit::getInstance();
$singleton->foo = 'bar';
var_dump($singleton);
$singleton = SomeShit::getInstance();
var_dump($singleton);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment