<?php

namespace MediaTrim\Traits;

/**
 * Trait Singleton
 *
 * A reusable singleton trait for classes.
 *
 * @package MediaTrim\Traits
 */
trait Singleton
{

    /**
     * Store instances of classes using this trait
     *
     * @var array
     */
    protected static $instances = array();

    /**
     * Get the singleton instance
     *
     * @param mixed ...$args Arguments to pass to the constructor.
     * @return static
     */
    public static function get_instance(...$args)
    {
        if (!isset(self::$instances[static::class])) {
            self::$instances[static::class] = !empty($args) ? new static(...$args) : new static();
        }

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

    /**
     * Protected constructor
     *
     * @param mixed ...$args Arguments passed to constructor.
     */
    protected function __construct(...$args)
    {
    }
}
