测试
This commit is contained in:
@@ -8,7 +8,10 @@ class BaseService
|
||||
{
|
||||
protected $userId;
|
||||
protected $userInfo;
|
||||
protected static $instance;
|
||||
/**
|
||||
* @var mixed 单例实例,确保该类只有一个全局实例
|
||||
*/
|
||||
private static mixed $_instance;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
@@ -17,13 +20,17 @@ class BaseService
|
||||
$this->userInfo = UserModel::where('id', $token)->first();
|
||||
}
|
||||
|
||||
// 单例
|
||||
|
||||
public static function getInstance()
|
||||
/**
|
||||
* 获取实例
|
||||
* @return null|static
|
||||
*/
|
||||
public static function getInstance(): null|static
|
||||
{
|
||||
if (!self::$instance instanceof self) {
|
||||
self::$instance = new self();
|
||||
$name = get_called_class();
|
||||
if (!isset(self::$_instance[$name])) {
|
||||
self::$_instance[$name] = new static();
|
||||
}
|
||||
return self::$instance;
|
||||
|
||||
return self::$_instance[$name];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user