<?php

namespace HulkPlugins\Woo\AccountPages\Core;

use RuntimeException;

defined( 'ABSPATH' ) || exit;

trait SingletonTrait {

	// Hold the instances for each class.
	private static array $instances = [];

	// Prevent direct instantiation
	final private function __construct() {}

	// Prevent cloning
	final protected function __clone() {}

	// Prevent unserializing
	final public function __wakeup() {
		throw new RuntimeException( 'Cannot unserialize singleton' );
	}

	/**
	 * Static method to get the Singleton instance
	 * @return static
	 */
	public static function getInstance(): self {
		$class = static::class;

		if ( ! isset( self::$instances[ $class ] ) ) {
			self::$instances[ $class ] = new static();
		}

		return self::$instances[ $class ];
	}
}
