mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-02-22 16:54:13 +01:00
45 lines
804 B
PHP
45 lines
804 B
PHP
<?php
|
|
|
|
namespace DesignPatterns\Creational\Multiton;
|
|
|
|
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()
|
|
{
|
|
}
|
|
}
|