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
{{ message }}
This repository was archived by the owner on Dec 26, 2023. It is now read-only.
Copy file name to clipboardExpand all lines: README.md
+82-24Lines changed: 82 additions & 24 deletions
Original file line number
Diff line number
Diff line change
@@ -2,25 +2,85 @@
2
2
3
3
Enumerated type for PHP by [Petr Knap].
4
4
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)
5
11
6
-
## What is enum?
7
12
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.
9
16
-- [Enumerated type - Wikipedia, The Free Encyclopedia]
10
17
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
12
81
13
-
####Enum declaration
82
+
### Enum declaration
14
83
```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
-
*/
24
84
class DayOfWeekEnum extends \PetrKnap\Php\Enum\AbstractEnum
25
85
{
26
86
protected function members()
@@ -38,7 +98,7 @@ class DayOfWeekEnum extends \PetrKnap\Php\Enum\AbstractEnum
38
98
}
39
99
```
40
100
41
-
####Enum usage
101
+
### Enum usage
42
102
```php
43
103
if (DayOfWeekEnum::FRIDAY() == DayOfWeekEnum::FRIDAY()) {
44
104
echo "This is OK.";
@@ -52,17 +112,15 @@ if (DayOfWeekEnum::FRIDAY() == DayOfWeekEnum::MONDAY()) {
0 commit comments