trait Singleton
{
    private static $instace = null;

    private function __construct()
    {
    }

    private function __clone()
    {
    }
    
    public static function getInstace(...$args)
    {
        if (self::$instace instanceof self) {

        } else {
            self::$instace = new static(...$args);
        }
        return self::$instace;
    }
}

然后在需要用到单例的地方use使用

class Config
{
    use Singleton;
    private $config;

    public function get($key)
    {
        
    }
}


// 获取Config实例,Config是个单例的
$config = Config::getInstace();

 

相关文章:

  • 2021-06-19
  • 2022-01-22
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-27
  • 2022-01-13
猜你喜欢
  • 2022-12-23
  • 2021-11-10
  • 2021-06-17
  • 2021-07-17
  • 2022-12-23
  • 2021-06-12
  • 2021-09-02
相关资源
相似解决方案