Pattern Singleton PHP

Simple singleton trait

trait Singleton
{
    static private $instance = null;

    private function __construct() { }
    private function __clone() { }

    static public function getInstance()
    {
        if (is_null(static::$instance)) {
            static::$instance = new static();
        }

        return static::$instance;
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *