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
+12-9Lines changed: 12 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -1,16 +1,18 @@
1
1
# PHP Enumeration classes
2
2
Implementation of [Enumeration Classes](https://docs.microsoft.com/en-us/dotnet/architecture/microservices/microservice-ddd-cqrs-patterns/enumeration-classes-over-enum-types) in PHP.
3
3
4
-
In contrast to [existing solutions](#existing-solutions) this implementation avoid usage of [Magic methods](https://www.php.net/manual/en/language.oop5.magic.php) and
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
-
Also, we use static properties that can utilize the power of [Typed Properties](https://wiki.php.net/rfc/typed_properties_v2).
6
+
Enumeration class holds reference to single Enum element represented as a object (singleton) to provide possiblity
7
+
to use strict (`===`) comparision between the values.
8
+
Also, it uses static properties that can utilize the power of [Typed Properties](https://wiki.php.net/rfc/typed_properties_v2).
7
9
The Enumeration Classes is much closer to other language implementations like [Java Enums](https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html)
8
10
and [Python Enums](https://docs.python.org/3/library/enum.html).
9
11
10
12
11
13
## Declaration
12
14
13
-
Basic way to declare named Enumeration class:
15
+
A basic way to declare named Enumeration class:
14
16
```php
15
17
<?php
16
18
use Dbalabka\Enumeration;
@@ -24,7 +26,7 @@ final class Action extends Enumeration
24
26
Action::initialize();
25
27
```
26
28
27
-
with Typed Properties support:
29
+
Declaration with Typed Properties support:
28
30
```php
29
31
<?php
30
32
final class Day extends Enumeration
@@ -114,15 +116,16 @@ By default enumeration class does not require the value to be provided. You can
114
116
```
115
117
116
118
Declaration rules that developer should follow:
117
-
1. You should always declare the Enum resulting enum class as `final`.
119
+
1. The resulting Enumeration class should be marked as `final`. Abstract classes should be used to share functional between
120
+
multiple Enumeration classes.
118
121
> ...Allowing subclassing of enums that define members would lead to a violation of some important invariants of types and instances.
119
122
> On the other hand, it makes sense to allow sharing some common behavior between a group of enumerations...
any static property that isn't Enum element than you should override `\Dbalabka\Enumeration::getStaticVars()` method.
127
+
4. Method `Dbalabka\Enumeration::initialize()` should be called after each Enumeration class declaration. Please use
128
+
[vladimmi/construct-static](https://github.com/vladimmi/construct-static) custom loader to avoid boilerplate code.
126
129
127
130
## Usage
128
131
```php
@@ -156,7 +159,7 @@ Action::$view = null;
156
159
157
160
### Class static initialization
158
161
Implementation rely on class static initialization which was proposed in [Static Class Constructor](https://wiki.php.net/rfc/static_class_constructor).
159
-
RFC describes the possible workarounds. Simplest is to call initialization method right after class declaration,
162
+
RFC describes the possible workarounds. The simplest way is to call initialization method right after class declaration,
160
163
but it requires keep this in mind. Thanks to [Typed Properties](https://wiki.php.net/rfc/typed_properties_v2)
161
164
we can control not initialized properties - PHP will throw and error in case of access to not initialized property.
162
165
It might be automated with custom autoloader implemented in [vladimmi/construct-static](https://github.com/vladimmi/construct-static) library.
0 commit comments