mirror of
https://github.com/opencultureconsulting/php-basics.git
synced 2025-04-06 00:00:31 +02:00
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;
|
namespace OCC\Basics\Traits;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
use LogicException;
|
use LogicException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -45,11 +46,17 @@ trait Singleton
|
|||||||
* @param mixed ...$args Constructor parameters
|
* @param mixed ...$args Constructor parameters
|
||||||
*
|
*
|
||||||
* @return static The singleton instance
|
* @return static The singleton instance
|
||||||
|
*
|
||||||
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
final public static function getInstance(mixed ...$args): static
|
final public static function getInstance(mixed ...$args): static
|
||||||
{
|
{
|
||||||
if (!isset(static::$singleton[static::class])) {
|
if (!isset(static::$singleton[static::class])) {
|
||||||
static::$singleton[static::class] = new static(...$args);
|
try {
|
||||||
|
static::$singleton[static::class] = new static(...$args);
|
||||||
|
} catch (Exception $exception) {
|
||||||
|
throw $exception;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return static::$singleton[static::class];
|
return static::$singleton[static::class];
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user