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

Commit 4a36da8

Browse files
committed
Update README.md
1 parent ecca6a2 commit 4a36da8

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,36 @@ Enumerated type for PHP by [Petr Knap].
88
> 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.
99
-- [Enumerated type - Wikipedia, The Free Encyclopedia]
1010

11+
## Why use enum?
12+
13+
Because it is safer then using class constants.
14+
15+
```php
16+
class MyBoolen
17+
{
18+
const MY_TRUE = 1;
19+
const MY_FALSE = 2;
20+
}
21+
22+
function IsTrue($myBoolean)
23+
{
24+
switch($myBoolean) {
25+
case MyBoolen::MY_TRUE:
26+
return true;
27+
case MyBoolen::MY_FALSE:
28+
return false;
29+
}
30+
}
31+
32+
IsTrue(MyBoolen::MY_TRUE); // returns true - OK
33+
IsTrue(MyBoolen::MY_FALSE); // returns false - OK
34+
IsTrue(1); // returns true - OK
35+
IsTrue(2); // returns false - scary, but ok
36+
IsTrue(true); // returns true - OK
37+
IsTrue(false); // returns null - WTF?
38+
```
39+
40+
1141
## Usage of php-enum
1242

1343
### Enum declaration

0 commit comments

Comments
 (0)