Files
hyperf_service/app/Service/Common/AppMakeService.php
2024-10-27 00:34:45 +08:00

46 lines
914 B
PHP

<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\Common;
use Exception;
use Hyperf\Context\Context;
class AppMakeService
{
/**
* 生成类对象
* @param $className
* @return mixed
* @throws Exception
*/
public static function make($className): mixed
{
if (!class_exists($className)) throw new Exception('The instantiated class does not exist');
$array = Context::get('obj');
if (!isset($array[$className])) {
$array = $array ?? [];
$array[$className] = new $className;
Context::set('obj', $array);
}
return $array[$className];
}
/**
* 获取对象类
* @return mixed
*/
public static function getObj(): mixed
{
return Context::get('obj');
}
}