1818class Enum implements EnumInterface
1919{
2020 /** @var array */
21- private static array $ constants ;
21+ private static array $ constants = [];
22+
23+ protected static array $ instances = [];
2224
2325 /** @var mixed */
2426 private mixed $ value ;
@@ -33,11 +35,10 @@ class Enum implements EnumInterface
3335 */
3436 final public function __construct (mixed $ value )
3537 {
36- static ::$ constants = static ::constants ();
37-
3838 $ value = ($ value instanceof EnumInterface) ? $ value ->getValue () : $ value ;
3939
4040 $ this ->key = static ::searchNameByValue ($ value );
41+
4142 $ this ->value = $ value ;
4243 }
4344
@@ -46,9 +47,15 @@ final public function __construct(mixed $value)
4647 */
4748 private static function constants (): array
4849 {
50+ if (isset (static ::$ constants [static ::class])) {
51+ return static ::$ constants [static ::class];
52+ }
53+
4954 $ reflection = new ReflectionClass (static ::class);
5055
51- return $ reflection ->getConstants ();
56+ static ::$ constants [static ::class] = $ reflection ->getConstants ();
57+
58+ return static ::$ constants [static ::class];
5259 }
5360
5461 /**
@@ -65,7 +72,7 @@ public function getValue(): mixed
6572 */
6673 private static function searchNameByValue (mixed $ value ): string
6774 {
68- $ name = array_search ($ value , static ::$ constants , true );
75+ $ name = array_search ($ value , static ::constants () , true );
6976 if (false === $ name ) {
7077 throw new InvalidArgumentException (sprintf ('Constant value [%s] not found ' , $ value ));
7178 }
@@ -80,27 +87,32 @@ private static function searchNameByValue(mixed $value): string
8087 */
8188 final public static function __callStatic (string $ name , array $ arguments ): self
8289 {
83- if (!array_key_exists ($ name , static ::$ constants )) {
90+ if (!array_key_exists ($ name , static ::constants () )) {
8491 throw new InvalidArgumentException (sprintf ('Constant [%s] is not defined ' , $ name ));
8592 }
93+ if (isset (static ::$ instances [static ::class][$ name ])) {
94+ return static ::$ instances [static ::class][$ name ];
95+ }
96+
97+ static ::$ instances [static ::class][$ name ] = new static (static ::constants ()[$ name ]);
8698
87- return new static ( static ::$ constants [ $ name ]) ;
99+ return static ::$ instances [ static ::class][ $ name ];
88100 }
89101
90102 /**
91103 * @return mixed[]
92104 */
93105 public function getValues (): array
94106 {
95- return array_values (static ::$ constants );
107+ return array_values (static ::$ constants[ static ::class] );
96108 }
97109
98110 /**
99111 * @return string[]
100112 */
101113 public function getKeys (): array
102114 {
103- return array_keys (static ::$ constants );
115+ return array_keys (static ::$ constants[ static ::class] );
104116 }
105117
106118 /**
@@ -116,7 +128,7 @@ public function getKey(): string
116128 */
117129 public function toArray (): array
118130 {
119- return static ::$ constants ;
131+ return static ::$ constants[ static ::class] ;
120132 }
121133
122134 /**
0 commit comments