You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Also, it would be very helpful to have expression based properties initialization:
194
+
```php
195
+
class Enum {
196
+
// this is not allowed
197
+
public static $FOO = new Enum();
198
+
public static $BAR = new Enum();
199
+
}
200
+
```
184
201
See [examples/class_static_construct.php](examples/class_static_construct.php) for example.
185
202
186
203
### Serialization
@@ -195,7 +212,7 @@ but it gives the possibility to control this in the class which holds the refere
195
212
with [Serializable Interface](https://www.php.net/manual/en/class.serializable.php) in a similar way.
196
213
For example, Java [handles](https://stackoverflow.com/questions/15521309/is-custom-enum-serializable-too/15522276#15522276) Enums serialization differently than other classes, but you can serialize it directly thanks to [readResolve()](https://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html).
197
214
In PHP, we can't serialize Enums directly, but we can handle Enums serialization in class that holds the reference. We can serialize the name of the Enum constant and use `valueOf()` method to obtain the Enum constant value during unserialization.
198
-
So this problem somewhat resolved a the cost of a worse developer experience. Hope it will be solved in future RFCs.
215
+
So this problem somewhat resolved the cost of a worse developer experience. Hope it will be solved in future RFCs.
199
216
```php
200
217
class SomeClass
201
218
{
@@ -212,7 +229,36 @@ class SomeClass
212
229
}
213
230
}
214
231
```
215
-
See complete example in [examples/serialization_php74.php](examples/serialization_php74.php).
232
+
See complete example in [examples/serialization_php74.php](examples/serialization_php74.php).
233
+
234
+
### Callable static properties syntax
235
+
Unfortunately, you can not simply use static property as callable. There was a
236
+
[syntax change](https://wiki.php.net/rfc/uniform_variable_syntax#backward_incompatible_changes) since PHP 7.0.
237
+
```php
238
+
// Instead of using syntax
239
+
Option::$some('1'); // this line will rise an error "Function name must be a string"
240
+
// you should use
241
+
(Option::$some)('1');
242
+
```
243
+
It might be that using static variables isn't best option.
244
+
245
+
Probably the another option is to use magic calls with `__callStatic` but this variant suffers from missing autosuggestion,
246
+
negative performance impact and missing static analysis that might be overcome by additional manually added annotations.
247
+
```php
248
+
Option::some('1');
249
+
```
250
+
251
+
The best option is native PHP implementation. Unfortunately, it might be complicated task. It might seem that a quick solution it would be
252
+
helpful to have late (in runtime) constants initialization or/and expression based class constants initialization:
253
+
```php
254
+
class Enum {
255
+
// this is not allowed
256
+
public const FOO = new Enum();
257
+
public const BAR = new Enum();
258
+
}
259
+
```
260
+
Still, calling `Enum::FOO()` will try to find a method instead of trying to treat constant's value as a callable.
261
+
So we make a conclusion that enum syntax that use constants will not work but static properties will.
0 commit comments