From 3f666d724d4da8bf383d5269a4f942a453282734 Mon Sep 17 00:00:00 2001 From: Sebastian Meyer Date: Sun, 11 Feb 2024 16:21:02 +0100 Subject: [PATCH] Update phpDoc for Singleton trait --- src/Traits/Singleton.php | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/Traits/Singleton.php b/src/Traits/Singleton.php index 77f78d3..531b7f2 100644 --- a/src/Traits/Singleton.php +++ b/src/Traits/Singleton.php @@ -28,6 +28,15 @@ use LogicException; /** * 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 * @package Basics\Traits */ @@ -40,12 +49,12 @@ trait Singleton * * @internal */ - private static array $singleton = []; + private static array $_singleton = []; /** * Get a singleton instance of this class. * - * @param mixed ...$args Constructor parameters + * @param mixed ...$args Constructor arguments * * @return static The singleton instance * @@ -53,17 +62,15 @@ trait Singleton */ final public static function getInstance(mixed ...$args): static { - if (!isset(static::$singleton[static::class])) { - static::$singleton[static::class] = new static(...$args); + if (!isset(static::$_singleton[static::class])) { + 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. * - * Usage: Get an instance of this class by calling static::getInstance() - * * @return void */ abstract private function __construct();