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();