Skip to content

Commit 76444e5

Browse files
aheartdbalabka
authored andcommitted
Documentation and example improvements (#2)
1 parent edb6345 commit 76444e5

File tree

2 files changed

+39
-42
lines changed

2 files changed

+39
-42
lines changed

README.md

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ Implementation of [Enumeration Classes](https://docs.microsoft.com/en-us/dotnet/
33

44
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
55
[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
77
to use strict (`===`) comparision between the values.
88
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)
1010
and [Python Enums](https://docs.python.org/3/library/enum.html).
1111

1212

1313
## Declaration
1414

15-
A basic way to declare named Enumeration class:
15+
A basic way to declare a named Enumeration class:
1616
```php
1717
<?php
1818
use Dbalabka\Enumeration;
@@ -22,7 +22,7 @@ final class Action extends Enumeration
2222
public static $view;
2323
public static $edit;
2424
}
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
2626
Action::initialize();
2727
```
2828

@@ -42,7 +42,7 @@ final class Day extends Enumeration
4242
Day::initialize();
4343
```
4444

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.
4646
1. Flag enum implementation example:
4747
```php
4848
<?php
@@ -115,16 +115,16 @@ By default enumeration class does not require the value to be provided. You can
115115
}
116116
```
117117

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
120120
multiple Enumeration classes.
121121
> ...Allowing subclassing of enums that define members would lead to a violation of some important invariants of types and instances.
122122
> On the other hand, it makes sense to allow sharing some common behavior between a group of enumerations...
123123
> (from [Python Enum documentation](https://docs.python.org/3/library/enum.html#restricted-enum-subclassing))
124124
2. Constructor should always be declared as non-public (`private` or `protected`) to avoid unwanted class instantiation.
125125
3. Implementation is based on assumption that all class static properties are elements of Enum. If there is a need to declare
126-
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
126+
any static property that isn't an Enum element then you should override the `\Dbalabka\Enumeration::getStaticVars()` method.
127+
4. The method `Dbalabka\Enumeration::initialize()` should be called after each Enumeration class declaration. Please use the
128128
[vladimmi/construct-static](https://github.com/vladimmi/construct-static) custom loader to avoid boilerplate code.
129129

130130
## Usage
@@ -146,9 +146,9 @@ foreach (Action::values() as $name => $action) {
146146

147147
## Known issues
148148
### Readonly Properties
149-
In the current implementation, static property value might be occasionally replaced.
149+
In the current implementation, static property value can be occasionally replaced.
150150
[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.
152152
```php
153153
<?php
154154
// It is possible but don't do it
@@ -158,34 +158,32 @@ Action::$view = null;
158158
```
159159

160160
### 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.
165165
It might be automated with custom autoloader implemented in [vladimmi/construct-static](https://github.com/vladimmi/construct-static) library.
166166
```php
167167
<?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
170170
Action::initialize();
171171
```
172172
See [examples/class_static_construct.php](examples/class_static_construct.php) for example to overcome this limitation.
173173

174174
### 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.
176176
```php
177177
<?php
178-
// Following line will throw an exception
178+
// The following line will throw an exception
179179
serialize(Action::$view);
180180
```
181181
[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.
189187
```php
190188
class SomeClass
191189
{
@@ -205,13 +203,12 @@ class SomeClass
205203
See complete example in [examples/serialization_php74.php](examples/serialization_php74.php).
206204

207205
## Existing solutions
208-
In contrast to existing solutions and RFCs like
209206
* https://github.com/myclabs/php-enum
210207
* https://github.com/marc-mabe/php-enum
211208
* https://www.php.net/manual/en/class.splenum.php
212209
* [PHP RFC: Enumerated Types](https://wiki.php.net/rfc/enum)
213210

214-
(there a lot of [other PHP implementations](https://packagist.org/search/?query=php-enum))
211+
(there are a lot of [other PHP implementations](https://packagist.org/search/?query=php-enum))
215212

216213
## References
217214
* [Enum Types](https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html) - The Java™ Tutorials

examples/cartType.php renamed to examples/cardType.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
require_once(__DIR__ . '/../vendor/autoload.php');
77

8-
final class CartType extends Enumeration {
8+
final class CardType extends Enumeration {
99
public static $amex;
1010
public static $visa;
1111
public static $masterCard;
@@ -17,36 +17,36 @@ protected static function initializeValues() : void
1717
self::$masterCard = new self();
1818
}
1919
}
20-
CartType::initialize();
20+
CardType::initialize();
2121

22-
var_dump(CartType::values());
22+
var_dump(CardType::values());
2323

2424

25-
class Cart
25+
class Card
2626
{
2727
private $type;
2828

29-
public function __construct(CartType $type)
29+
public function __construct(CardType $type)
3030
{
3131
$this->type = $type;
3232
}
3333
}
3434

35-
$cart = new Cart(CartType::$amex);
36-
var_dump(CartType::$amex === CartType::$visa);
37-
var_dump(CartType::$amex === CartType::$amex);
35+
$card = new Card(CardType::$amex);
36+
var_dump(CardType::$amex === CardType::$visa);
37+
var_dump(CardType::$amex === CardType::$amex);
3838

39-
foreach (CartType::values() as $type) {
39+
foreach (CardType::values() as $type) {
4040
echo $type . "\n";
4141
}
4242

43-
$a = CartType::$visa;
43+
$a = CardType::$visa;
4444
switch ($a) {
45-
case CartType::$amex:
46-
echo 'It is ' . CartType::$amex . PHP_EOL;
45+
case CardType::$amex:
46+
echo 'It is ' . CardType::$amex . PHP_EOL;
4747
break;
48-
case CartType::$visa:
49-
echo 'It is ' . CartType::$visa . PHP_EOL;
48+
case CardType::$visa:
49+
echo 'It is ' . CardType::$visa . PHP_EOL;
5050
break;
5151
}
5252

0 commit comments

Comments
 (0)