Add exception handling to Singleton trait

This commit is contained in:
Sebastian Meyer 2024-01-03 15:27:36 +01:00
parent 6ba215e3fc
commit b20a8df00c
1 changed files with 8 additions and 1 deletions

View File

@ -22,6 +22,7 @@ declare(strict_types=1);
namespace OCC\Basics\Traits;
use Exception;
use LogicException;
/**
@ -45,11 +46,17 @@ trait Singleton
* @param mixed ...$args Constructor parameters
*
* @return static The singleton instance
*
* @throws Exception
*/
final public static function getInstance(mixed ...$args): static
{
if (!isset(static::$singleton[static::class])) {
try {
static::$singleton[static::class] = new static(...$args);
} catch (Exception $exception) {
throw $exception;
}
}
return static::$singleton[static::class];
}