mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-25 09:11:17 +02:00
45 lines
835 B
PHP
45 lines
835 B
PHP
<?php declare(strict_types=1);
|
|
|
|
namespace DesignPatterns\Creational\Multiton;
|
|
|
|
final class Multiton
|
|
{
|
|
const INSTANCE_1 = '1';
|
|
const INSTANCE_2 = '2';
|
|
|
|
/**
|
|
* @var Multiton[]
|
|
*/
|
|
private static $instances = [];
|
|
|
|
/**
|
|
* this is private to prevent from creating arbitrary instances
|
|
*/
|
|
private function __construct()
|
|
{
|
|
}
|
|
|
|
public static function getInstance(string $instanceName): Multiton
|
|
{
|
|
if (!isset(self::$instances[$instanceName])) {
|
|
self::$instances[$instanceName] = new self();
|
|
}
|
|
|
|
return self::$instances[$instanceName];
|
|
}
|
|
|
|
/**
|
|
* prevent instance from being cloned
|
|
*/
|
|
private function __clone()
|
|
{
|
|
}
|
|
|
|
/**
|
|
* prevent instance from being unserialized
|
|
*/
|
|
private function __wakeup()
|
|
{
|
|
}
|
|
}
|