This commit is contained in:
2025-06-26 10:05:04 +08:00
parent 0f57d5a04a
commit 28f0dd0c06
4 changed files with 25 additions and 19 deletions

View File

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