File tree 2 files changed +26
-1
lines changed
2 files changed +26
-1
lines changed Original file line number Diff line number Diff line change @@ -24,6 +24,13 @@ abstract class Enum
24
24
*/
25
25
protected $ value ;
26
26
27
+ /**
28
+ * Enum value instance
29
+ *
30
+ * @var mixed
31
+ */
32
+ protected static $ instances = array ();
33
+
27
34
/**
28
35
* Store existing constants in a static cache per object.
29
36
*
@@ -177,8 +184,15 @@ public static function search($value)
177
184
public static function __callStatic ($ name , $ arguments )
178
185
{
179
186
$ array = static ::toArray ();
187
+ $ class = get_called_class ();
180
188
if (isset ($ array [$ name ])) {
181
- return new static ($ array [$ name ]);
189
+ if (isset (static ::$ instances [$ class ][$ name ])) {
190
+ return static ::$ instances [$ class ][$ name ];
191
+ } else {
192
+ $ result = new static ($ array [$ name ]);
193
+ static ::$ instances [$ class ][$ name ] = $ result ;
194
+ return $ result ;
195
+ }
182
196
}
183
197
184
198
throw new \BadMethodCallException ("No static method or enum constant ' $ name' in class " . get_called_class ());
Original file line number Diff line number Diff line change @@ -225,6 +225,17 @@ public function testEquals()
225
225
$ this ->assertTrue ($ foo ->equals ($ anotherFoo ));
226
226
}
227
227
228
+ /**
229
+ * __callStatic()
230
+ */
231
+ public function testSameInstance ()
232
+ {
233
+ $ foo1 = EnumFixture::FOO ();
234
+ $ foo2 = EnumFixture::FOO ();
235
+
236
+ $ this ->assertSame ($ foo1 , $ foo2 );
237
+ }
238
+
228
239
/**
229
240
* equals()
230
241
*/
You can’t perform that action at this time.
0 commit comments