Skip to content
This repository was archived by the owner on Dec 26, 2023. It is now read-only.

Commit b614f6d

Browse files
author
Petr Knap
committed
Update README.md
2 parents 7830110 + 52964f5 commit b614f6d

File tree

1 file changed

+82
-24
lines changed

1 file changed

+82
-24
lines changed

README.md

Lines changed: 82 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,85 @@
22

33
Enumerated type for PHP by [Petr Knap].
44

5+
* [What is Enum?](#what-is-enum)
6+
* [Why use Enums instead of Constants?](#why-use-enums-instead-of-constants)
7+
* [Usage of php-enum](#usage-of-php-enum)
8+
* [Enum declaration](#enum-declaration)
9+
* [Enum usage](#enum-usage)
10+
* [How to install](#how-to-install)
511

6-
## What is enum?
712

8-
> In computer programming, an **enumerated type** (also called **enumeration** or **enum**, or **factor** in the R programming language, and a categorical variable in statistics) is a data type consisting of a set of named values called **elements**, **members**, **enumeral**, or **enumerators** of the type. The enumerator names are usually identifiers that behave as constants in the language. A variable that has been declared as having an enumerated type can be assigned any of the enumerators as a value. In other words, an *enumerated type has values that are different from each other*, and that can be compared and assigned, but which are not specified by the programmer as having any particular concrete representation in the computer's memory; compilers and interpreters can represent them arbitrarily.
13+
## What is Enum?
14+
15+
> In computer programming, an **enumerated type** (also called **enumeration** or **enum**, or **factor** in the R programming language, and a **categorical variable** in statistics) is a data type consisting of a set of named values called **elements**, **members**, **enumeral**, or **enumerators** of the type. The enumerator names are usually identifiers that behave as constants in the language. A variable that has been declared as having an enumerated type can be assigned any of the enumerators as a value. In other words, an *enumerated type has values that are different from each other*, and that can be compared and assigned, but which are not specified by the programmer as having any particular concrete representation in the computer's memory; compilers and interpreters can represent them arbitrarily.
916
-- [Enumerated type - Wikipedia, The Free Encyclopedia]
1017

11-
### Usage of php-enum
18+
## Why use Enums instead of Constants?
19+
20+
Because **it is safer and less scary** than using constants. Don't trust me? Let see at this code:
21+
22+
```php
23+
class MyBoolean
24+
{
25+
const MY_TRUE = 1;
26+
const MY_FALSE = 2;
27+
}
28+
29+
function isTrue(int $myBoolean)
30+
{
31+
switch($myBoolean) {
32+
case MyBoolean::MY_TRUE:
33+
return true;
34+
case MyBoolean::MY_FALSE:
35+
return false;
36+
}
37+
}
38+
39+
isTrue(MyBoolean::MY_TRUE); // returns true - OK
40+
isTrue(MyBoolean::MY_FALSE); // returns false - OK
41+
isTrue(1); // returns true - OK
42+
isTrue(2); // returns false - scary, but OK
43+
isTrue(true); // returns true - OK
44+
isTrue(false); // returns null - WTF?
45+
```
46+
47+
And now the **same code with Enum** instead of Constants:
48+
49+
```php
50+
class MyBoolean extends \PetrKnap\Php\Enum\AbstractEnum
51+
{
52+
protected function members()
53+
{
54+
return [
55+
"MY_TRUE" => 1,
56+
"MY_FALSE" => 2
57+
];
58+
}
59+
}
60+
61+
function isTrue(MyBoolean $myBoolean)
62+
{
63+
switch($myBoolean) {
64+
case MyBoolean::MY_TRUE():
65+
return true;
66+
case MyBoolean::MY_FALSE():
67+
return false;
68+
}
69+
}
70+
71+
isTrue(MyBoolean::MY_TRUE()); // returns true - OK
72+
isTrue(MyBoolean::MY_FALSE()); // returns false - OK
73+
isTrue(1); // uncaught type error - OK
74+
isTrue(2); // uncaught type error - OK
75+
isTrue(true); // uncaught type error - OK
76+
isTrue(false); // uncaught type error - OK
77+
```
78+
79+
80+
## Usage of php-enum
1281

13-
#### Enum declaration
82+
### Enum declaration
1483
```php
15-
/**
16-
* @method static DayOfWeekEnum SUNDAY()
17-
* @method static DayOfWeekEnum MONDAY()
18-
* @method static DayOfWeekEnum TUESDAY()
19-
* @method static DayOfWeekEnum WEDNESDAY()
20-
* @method static DayOfWeekEnum THURSDAY()
21-
* @method static DayOfWeekEnum FRIDAY()
22-
* @method static DayOfWeekEnum SATURDAY()
23-
*/
2484
class DayOfWeekEnum extends \PetrKnap\Php\Enum\AbstractEnum
2585
{
2686
protected function members()
@@ -38,7 +98,7 @@ class DayOfWeekEnum extends \PetrKnap\Php\Enum\AbstractEnum
3898
}
3999
```
40100

41-
#### Enum usage
101+
### Enum usage
42102
```php
43103
if (DayOfWeekEnum::FRIDAY() == DayOfWeekEnum::FRIDAY()) {
44104
echo "This is OK.";
@@ -52,17 +112,15 @@ if (DayOfWeekEnum::FRIDAY() == DayOfWeekEnum::MONDAY()) {
52112
```
53113

54114
```php
55-
switch($dayOfWeek)
115+
function isWeekend(DayOfWeekEnum $dayOfWeek)
56116
{
57-
case DayOfWeekEnum::FRIDAY():
58-
echo "Finally it is Friday!";
59-
break;
60-
case DayOfWeekEnum::SATURDAY():
61-
case DayOfWeekEnum::SUNDAY():
62-
echo "It is leasure time!";
63-
break;
64-
default:
65-
echo "Just another working day...";
117+
switch ($dayOfWeek) {
118+
case DayOfWeekEnum::SATURDAY():
119+
case DayOfWeekEnum::SUNDAY():
120+
return true;
121+
default:
122+
return false;
123+
}
66124
}
67125
```
68126

0 commit comments

Comments
 (0)