Skip to content

Commit adcbb65

Browse files
committed
Update Readme
1 parent 7bb84a6 commit adcbb65

File tree

1 file changed

+18
-21
lines changed

1 file changed

+18
-21
lines changed

README.md

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@ class Action extends Enum
4242
## Usage
4343

4444
```php
45-
$action = new Action(Action::VIEW);
46-
47-
// or
4845
$action = Action::VIEW();
4946
```
5047

@@ -58,9 +55,22 @@ function setAction(Action $action) {
5855
}
5956
```
6057

58+
Each Enum instance for a given key is a singleton, so you can use:
59+
60+
```php
61+
function setAction(Action $action) {
62+
if ($action === Action::VIEW()) {
63+
//
64+
}
65+
}
66+
```
67+
68+
**Note** that this is not true, if you `unserialize()` Enums.
69+
In case another Enum instance already exists,
70+
an `E_USER_NOTICE` is triggered.
71+
6172
## Documentation
6273

63-
- `__construct()` The constructor checks that the value exist in the enum
6474
- `__toString()` You can `echo $myValue`, it will display the enum value (value of the constant)
6575
- `getValue()` Returns the current value of the enum
6676
- `getKey()` Returns the key of the current value on Enum
@@ -74,6 +84,8 @@ Static methods:
7484
- `isValid()` Check if tested value is valid on enum set
7585
- `isValidKey()` Check if tested key is valid on enum set
7686
- `search()` Return key for searched value
87+
- `fromKey()` Return Enum instance for the given key
88+
- `fromValue()` Return Enum instance for the given value
7789

7890
### Static methods
7991

@@ -91,23 +103,8 @@ $action = Action::EDIT();
91103

92104
Static method helpers are implemented using [`__callStatic()`](http://www.php.net/manual/en/language.oop5.overloading.php#object.callstatic).
93105

94-
If you care about IDE autocompletion, you can either implement the static methods yourself:
95-
96-
```php
97-
class Action extends Enum
98-
{
99-
const VIEW = 'view';
100-
101-
/**
102-
* @return Action
103-
*/
104-
public static function VIEW() {
105-
return new Action(self::VIEW);
106-
}
107-
}
108-
```
109-
110-
or you can use phpdoc (this is supported in PhpStorm for example):
106+
If you care about IDE autocompletion,
107+
you can use phpdoc (this is supported in PhpStorm for example):
111108

112109
```php
113110
/**

0 commit comments

Comments
 (0)