0

I have a class which I am going to make Singleton. For this reason, I want to use a trait. Something like this:

trait TSingleton
{
    private static $instance = null;

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

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

        return static::$instance;
    }
}

And then:

class Db implements IDb
{
    use TSingleton;
    ...
}

The question is if the Db's constructor would be private also, and if it does, why could I create its inheritors?

kosemMG
  • 1
  • 1
  • why not [testing it](https://3v4l.org/5kc4s)? (spoiler: it's private too) But [you can override it](https://3v4l.org/WPG5o) – Jeff Jan 15 '19 at 23:16
  • https://stackoverflow.com/questions/12755539/why-is-singleton-considered-an-anti-pattern Is probably worth a read. – Jonnix Jan 15 '19 at 23:21

0 Answers0