We recently had a discussion if it was possible to build a trait Singleton PHP Traits and we played around with it a possible Implementation but ran into issues
This is guys all what you need. If you wish you could use private static member, but there is no a real need... Tested, works despite the fact that you might think that static will be global or something :)
trait Singleton
{
/**
* Singleton pattern implementation
* @return mixed
*/
public static function Instance()
{
static $instance = null;
if (is_null($instance)) {
$instance = new self();
}
return $instance;
}
}
usage:
class MyClass
{
use Singleton;
}