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
Copy file name to clipboardExpand all lines: README.md
+25-28Lines changed: 25 additions & 28 deletions
Original file line number
Diff line number
Diff line change
@@ -3,16 +3,16 @@ Implementation of [Enumeration Classes](https://docs.microsoft.com/en-us/dotnet/
3
3
4
4
In contrast to [existing solutions](#existing-solutions), this implementation avoids usage of [Magic methods](https://www.php.net/manual/en/language.oop5.magic.php) and
5
5
[Reflection](https://www.php.net/manual/en/book.reflection.php) to provide better performance and code autocompletion.
6
-
Enumeration class holds reference to single Enum element represented as a object (singleton) to provide possiblity
6
+
The Enumeration class holds a reference to a single Enum element represented as an object (singleton) to provide the possiblity
7
7
to use strict (`===`) comparision between the values.
8
8
Also, it uses static properties that can utilize the power of [Typed Properties](https://wiki.php.net/rfc/typed_properties_v2).
9
-
The Enumeration Classes is much closer to other language implementations like [Java Enums](https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html)
9
+
The Enumeration Classes are much closer to other language implementations like [Java Enums](https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html)
10
10
and [Python Enums](https://docs.python.org/3/library/enum.html).
11
11
12
12
13
13
## Declaration
14
14
15
-
A basic way to declare named Enumeration class:
15
+
A basic way to declare a named Enumeration class:
16
16
```php
17
17
<?php
18
18
use Dbalabka\Enumeration;
@@ -22,7 +22,7 @@ final class Action extends Enumeration
22
22
public static $view;
23
23
public static $edit;
24
24
}
25
-
// to avoid manual initialization you can setup "vladimmi/construct-static" custom loader
25
+
// to avoid manual initialization you can setup the "vladimmi/construct-static" custom loader
26
26
Action::initialize();
27
27
```
28
28
@@ -42,7 +42,7 @@ final class Day extends Enumeration
42
42
Day::initialize();
43
43
```
44
44
45
-
By default enumeration class does not require the value to be provided. You can use constructor to set any types of values.
45
+
By default an enumeration class does not require the value to be provided. You can use the constructor to set any types of values.
46
46
1. Flag enum implementation example:
47
47
```php
48
48
<?php
@@ -115,16 +115,16 @@ By default enumeration class does not require the value to be provided. You can
115
115
}
116
116
```
117
117
118
-
Declaration rules that developer should follow:
119
-
1. The resulting Enumeration class should be marked as `final`. Abstract classes should be used to share functional between
118
+
Declaration rules that the developer should follow:
119
+
1. The resulting Enumeration class should be marked as `final`. Abstract classes should be used to share functionality between
120
120
multiple Enumeration classes.
121
121
> ...Allowing subclassing of enums that define members would lead to a violation of some important invariants of types and instances.
122
122
> On the other hand, it makes sense to allow sharing some common behavior between a group of enumerations...
In the current implementation, static property value might be occasionally replaced.
149
+
In the current implementation, static property value can be occasionally replaced.
150
150
[Readonly Properties](https://wiki.php.net/rfc/readonly_properties) is aimed to solve this issue.
151
-
In ideal world Enum values should be declared as a constants. Unfortunately, it is not possible in PHP right now.
151
+
In an ideal world Enum values should be declared as a constants. Unfortunately, it is not possible in PHP right now.
152
152
```php
153
153
<?php
154
154
// It is possible but don't do it
@@ -158,34 +158,32 @@ Action::$view = null;
158
158
```
159
159
160
160
### Class static initialization
161
-
Implementation rely on class static initialization which was proposed in [Static Class Constructor](https://wiki.php.net/rfc/static_class_constructor).
162
-
RFC describes the possible workarounds. The simplest way is to call initialization method right after class declaration,
163
-
but it requires keep this in mind. Thanks to [Typed Properties](https://wiki.php.net/rfc/typed_properties_v2)
164
-
we can control not initialized properties - PHP will throw and error in case of access to not initialized property.
161
+
This implementation relies on class static initialization which was proposed in [Static Class Constructor](https://wiki.php.net/rfc/static_class_constructor).
162
+
The RFC describes possible workarounds. The simplest way is to call the initialization method right after class declaration,
163
+
but it requires the developer to keep this in mind. Thanks to [Typed Properties](https://wiki.php.net/rfc/typed_properties_v2)
164
+
we can control uninitialized properties - PHP will throw and error in case of access to an uninitialized property.
165
165
It might be automated with custom autoloader implemented in [vladimmi/construct-static](https://github.com/vladimmi/construct-static) library.
166
166
```php
167
167
<?php
168
-
// You should always call initialize() method right after class declaration
169
-
// To avoid manual initialization you can setup "vladimmi/construct-static" custom loader
168
+
// You should always call the initialize() method right after class declaration
169
+
// To avoid manual initialization you can setup the "vladimmi/construct-static" custom loader
170
170
Action::initialize();
171
171
```
172
172
See [examples/class_static_construct.php](examples/class_static_construct.php) for example to overcome this limitation.
173
173
174
174
### Serialization
175
-
There no possibility to serialize the singleton. As a result, we have to restrict direct Enum object serialization.
175
+
There is no possibility to serialize the singleton. As a result, we have to restrict direct Enum object serialization.
176
176
```php
177
177
<?php
178
-
// Following line will throw an exception
178
+
// The following line will throw an exception
179
179
serialize(Action::$view);
180
180
```
181
181
[New custom object serialization mechanism](https://wiki.php.net/rfc/custom_object_serialization) does not help with singleton serialization
182
-
but it give the possibility to control this in class which hold the reference to Enums instances. Also, it can be workaround
183
-
with [Serializable Interface](https://www.php.net/manual/en/class.serializable.php) in similar way.
184
-
[Similar to Java Enums](https://stackoverflow.com/a/15522276/983577)
185
-
the PHP Enumeration Class serialized differently. For example [Java Enums](https://docs.oracle.com/javase/7/docs/api/java/lang/Enum.html)
186
-
implements Serializable interface and replace class instance during unserialization in [readResolve()](https://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html) method.
187
-
In PHP, serialize the name of Enum constant and use valueOf() method to obtain the Enum constant value during unserialization.
188
-
So this problem somehow solves with worse developer experience. Hope it will be solved in in future RFCs.
182
+
but it gives the possibility to control this in the class which holds the references to Enum instances. Also, it can be worked around
183
+
with [Serializable Interface](https://www.php.net/manual/en/class.serializable.php) in a similar way.
184
+
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).
185
+
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.
186
+
So this problem somewhat resolved a the cost of a worse developer experience. Hope it will be solved in future RFCs.
189
187
```php
190
188
class SomeClass
191
189
{
@@ -205,13 +203,12 @@ class SomeClass
205
203
See complete example in [examples/serialization_php74.php](examples/serialization_php74.php).
0 commit comments