<?php

namespace HulkPlugins\Woo\ProductConditionManager\Traits;

use RuntimeException;

defined( 'ABSPATH' ) || exit;

trait SingletonTrait {

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

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

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

	public function __wakeup() {
		throw new RuntimeException( 'Cannot unserialize Singleton' );
	}

	// Static method to get the Singleton instance
	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 ];
	}
}
