trait Singleton

Trait Singleton, This class ensures that all class that use this have only one instance of itself if called as: Class::getInstance()

If any specific class need more than one instance (for example database connection), can add a reference name to the function to have a separate configuration using this reference name.

Properties

static protected bool $singletonArray Set to true if you want use more that one singleton using and index param in getInstance
static protected string $basePath The base path where config files are placed.
static private string $className Name of the class
static private array $instances Hold the classes on instance.
protected bool $separateConfigFile Set to true if you want to save configuration in a separate file

Methods

static mixed
getInstance(string $index = 'main')

The object is created from within the class itself only if the class has no instance.

static string
getClassName()

Returns the class name.

bool
setConfig(array $params, bool $merge = true, string $index = 'main')

Save config to file.

array
getYamlContent()

Returns the content of the Yaml file.

string
getFilePath()

Return the full file config path.

string
getFileName()

Return the file name.

static string
yamlName()

Return the classname for yaml file.

bool
fileExists(string $filename)

Returns if file exists.

array
getConfig(string $index = 'main')

Returns the yaml config params.

static array
getDefaultValues()

Return default values

string
getBasePath()

Return the base path.

initSingleton()

Initialization, equivalent to __construct and must be called from main class.

Details

at line 72
static mixed getInstance(string $index = 'main')

The object is created from within the class itself only if the class has no instance.

We opted to use an array to make several singletons according to the index passed to getInstance

Parameters

string $index

Return Value

mixed

at line 88
static private string getClassName()

Returns the class name.

Return Value

string

at line 103
bool setConfig(array $params, bool $merge = true, string $index = 'main')

Save config to file.

Parameters

array $params
bool $merge
string $index

Return Value

bool

at line 130
private array getYamlContent()

Returns the content of the Yaml file.

Return Value

array

at line 152
string getFilePath()

Return the full file config path.

Return Value

string

at line 162
string getFileName()

Return the file name.

Return Value

string

at line 172
static string yamlName()

Return the classname for yaml file.

Return Value

string

at line 184
protected bool fileExists(string $filename)

Returns if file exists.

Parameters

string $filename

Return Value

bool

at line 196
array getConfig(string $index = 'main')

Returns the yaml config params.

Parameters

string $index

Return Value

array

at line 208
abstract static array getDefaultValues()

Return default values

Return Value

array

at line 215
string getBasePath()

Return the base path.

Return Value

string

at line 223
protected initSingleton()

Initialization, equivalent to __construct and must be called from main class.