Add exception handling to Singleton trait
This commit is contained in:
parent
6ba215e3fc
commit
b20a8df00c
|
@ -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];
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue