<?php

namespace HulkPlugins\ElementorPro\Forms\AdvancedFields\Traits;

use Exception;
use RuntimeException;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

trait SingletonTrait {

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

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

	// Prevent instance cloning
	private function __clone() {}

	/**
	 * @throws Exception
	 */
	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;  // Get the name of the class using the trait
		if ( ! isset( self::$instances[ $class ] ) ) {
			self::$instances[ $class ] = new static();  // Create the instance
		}
		return self::$instances[ $class ];
	}
}
