使用特征构造器

时间:2019-01-15 23:04:36

标签: php pdo constructor static singleton

我有一堂课,我要去做Singleton。因此,我想使用一个特征。像这样:

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;
    }
}

然后:

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

问题是Db的构造函数是否也将是私有的,如果是,为什么还要创建它的继承者?

0 个答案:

没有答案