<?php
/**
 * Abstract singleton base class.
 *
 * Subclasses call ChildClass::init() to get the single instance.
 * The constructor is protected so subclasses can define their own
 * bootstrap logic (hook registration, etc.).
 *
 * @package CoffeeWP_Media_Cleanup
 */

namespace CWPM;

defined( 'ABSPATH' ) || exit;

abstract class Singleton {

	/**
	 * Singleton instances keyed by class name.
	 *
	 * @var array<string, static>
	 */
	private static $instances = array();

	/**
	 * Get or create the singleton instance.
	 *
	 * @return static
	 */
	public static function init() {
		$class = static::class;
		if ( ! isset( self::$instances[ $class ] ) ) {
			self::$instances[ $class ] = new static();
		}
		return self::$instances[ $class ];
	}

	/**
	 * Protected constructor -- subclasses define their own.
	 */
	protected function __construct() {}
}
