mc-cms-namelessmc/core/classes/Core/Instanceable.php

31 lines
689 B
PHP
Raw Permalink Normal View History

<?php
/**
* Allows classes to extend this to make singleton instances easily.
*
* @package NamelessMC\Core
* @author Aberdeener
* @version 2.0.0-pr13
* @license MIT
*/
class Instanceable
{
/**
* Stores instances of classes with their class name as key.
*
2022-03-10 12:13:31 -08:00
* @var array<class-string, static>
*/
2021-12-07 22:14:12 -08:00
private static array $_instances = [];
/**
* Get or make an instance of the class this was called on.
2021-12-07 22:14:12 -08:00
*
* @return static Instance of the class this was called on.
*/
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();
}
}