2021-10-10 23:06:30 -07:00
|
|
|
<?php
|
|
|
|
|
|
2022-02-14 20:48:15 -08:00
|
|
|
/**
|
|
|
|
|
* Allows classes to extend this to make singleton instances easily.
|
|
|
|
|
*
|
|
|
|
|
* @package NamelessMC\Core
|
|
|
|
|
* @author Aberdeener
|
|
|
|
|
* @version 2.0.0-pr13
|
|
|
|
|
* @license MIT
|
|
|
|
|
*/
|
2024-03-09 03:19:55 -08:00
|
|
|
class Instanceable
|
|
|
|
|
{
|
2021-10-10 23:06:30 -07:00
|
|
|
/**
|
|
|
|
|
* Stores instances of classes with their class name as key.
|
2022-02-14 20:48:15 -08:00
|
|
|
*
|
2022-03-10 12:13:31 -08:00
|
|
|
* @var array<class-string, static>
|
2021-10-10 23:06:30 -07:00
|
|
|
*/
|
2021-12-07 22:14:12 -08:00
|
|
|
private static array $_instances = [];
|
2021-10-10 23:06:30 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get or make an instance of the class this was called on.
|
2021-12-07 22:14:12 -08:00
|
|
|
*
|
2022-02-14 20:48:15 -08:00
|
|
|
* @return static Instance of the class this was called on.
|
2021-10-10 23:06:30 -07:00
|
|
|
*/
|
2024-03-09 03:19:55 -08:00
|
|
|
final public static function getInstance()
|
|
|
|
|
{
|
2022-02-01 20:20:53 -08:00
|
|
|
/** @phpstan-ignore-next-line */
|
2021-12-07 22:14:12 -08:00
|
|
|
return self::$_instances[static::class] ??= new static();
|
2021-10-10 23:06:30 -07:00
|
|
|
}
|
|
|
|
|
}
|