Update phpDoc for Singleton trait

This commit is contained in:
Sebastian Meyer 2024-02-11 16:21:02 +01:00
parent 8fe5c94c39
commit 3f666d724d
1 changed files with 14 additions and 7 deletions

View File

@ -28,6 +28,15 @@ use LogicException;
/** /**
* Allows just a single instance of the class using this trait. * Allows just a single instance of the class using this trait.
* *
* Get the singleton object by calling the static method `getInstance()`.
*
* If there is no object yet, the constructor is called with the same arguments
* as `getInstance()`. Any later call will just return the already instantiated
* object (irrespective of the given arguments).
*
* In order for this to work as expected, the constructor has to be implemented
* as `private` to prevent direct instantiation of the class.
*
* @author Sebastian Meyer <sebastian.meyer@opencultureconsulting.com> * @author Sebastian Meyer <sebastian.meyer@opencultureconsulting.com>
* @package Basics\Traits * @package Basics\Traits
*/ */
@ -40,12 +49,12 @@ trait Singleton
* *
* @internal * @internal
*/ */
private static array $singleton = []; private static array $_singleton = [];
/** /**
* Get a singleton instance of this class. * Get a singleton instance of this class.
* *
* @param mixed ...$args Constructor parameters * @param mixed ...$args Constructor arguments
* *
* @return static The singleton instance * @return static The singleton instance
* *
@ -53,17 +62,15 @@ trait Singleton
*/ */
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); static::$_singleton[static::class] = new static(...$args);
} }
return static::$singleton[static::class]; return static::$_singleton[static::class];
} }
/** /**
* This is a singleton class, thus the constructor is private. * This is a singleton class, thus the constructor is private.
* *
* Usage: Get an instance of this class by calling static::getInstance()
*
* @return void * @return void
*/ */
abstract private function __construct(); abstract private function __construct();