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
An Enum defines a new type, which has a fixed, limited number of possible legal values.
1556
+
1557
+
```php
1558
+
enum Status
1559
+
{
1560
+
case DRAFT;
1561
+
case PUBLISHED;
1562
+
case ARCHIVED;
1563
+
}
1564
+
```
1565
+
1566
+
In an Enum, each case definition is case-sensitive. Historically, in PHP we generally represent "constants" with uppercase to distinguish them from normal variables, so it makes sense to stick to uppercase notation for enum cases. But note that this will work fine and define 3 different cases:
1567
+
1568
+
```php
1569
+
enum MyEnum
1570
+
{
1571
+
case FOO;
1572
+
case foo;
1573
+
case Foo;
1574
+
}
1575
+
```
1576
+
1577
+
Now you can compare easily enums with type safe operator `===`:
1578
+
1579
+
```php
1580
+
$statusA = Status::PENDING;
1581
+
1582
+
if ($statusA === Status::PENDING) {
1583
+
// true
1584
+
}
1585
+
```
1586
+
1587
+
Also an enum behaves like a traditional PHP object:
1588
+
1589
+
```php
1590
+
$statusA = Status::PENDING;
1591
+
$statusB = Status::PENDING;
1592
+
$statusC = Status::ARCHIVED;
1593
+
1594
+
$statusA === $statusB; // true
1595
+
$statusA === $statusC; // false
1596
+
$statusC instanceof Status; // true
1597
+
```
1598
+
1599
+
You can use Enum to enforce types:
1600
+
1601
+
```php
1602
+
function myFunction(Status $param)
1603
+
{
1604
+
return $param;
1605
+
}
1606
+
$a = myFunction(Status::DRAFT);
1607
+
// $a = Status::DRAFT
1608
+
$b = myFunction('foo'); // TypeError: myFunction(): Argument #1 ($param) must be of type Status, string given
1609
+
```
1610
+
1611
+
### Enum methods
1612
+
1613
+
You can define methods with an Enum :
1614
+
1615
+
```php
1616
+
enum Status
1617
+
{
1618
+
case DRAFT;
1619
+
case PUBLISHED;
1620
+
1621
+
public function label(): string
1622
+
{
1623
+
return match($this)
1624
+
{
1625
+
Status::DRAFT => 'Not ready...',
1626
+
Status::PUBLISHED => 'Published !',
1627
+
};
1628
+
}
1629
+
}
1630
+
```
1631
+
1632
+
then you can use methods on any enum instance:
1633
+
1634
+
```php
1635
+
$a = Status::DRAFT;
1636
+
$a->label(); // 'Not ready...'
1637
+
```
1638
+
1639
+
### Backed values
1640
+
1641
+
Sometimes you need to assign a proper value to each enum case (ex: to store it in a database, comparison, etc). You should define the type of the back value. Here is an example with a backed value defined as an `int` :
1642
+
1643
+
```php
1644
+
enum HttpStatus: int
1645
+
{
1646
+
case OK = 200;
1647
+
case NOT_FOUND = 404;
1648
+
case INTERNAL_SERVER_ERROR = 500;
1649
+
}
1650
+
```
1651
+
1652
+
And here is an example of a backed value defined as a `string`:
1653
+
1654
+
```php
1655
+
enum Status: string
1656
+
{
1657
+
case DRAFT = 'draft';
1658
+
case PUBLISHED = 'published';
1659
+
}
1660
+
```
1661
+
1662
+
#### External resource
1663
+
1664
+
-[Enums manual on PHP official documentation](https://www.php.net/manual/en/language.enumerations.php)
1665
+
-[Enums on PHP.Watch](https://php.watch/versions/8.0/match-expression)
1666
+
-[Enums style guide on stitcher's blog](https://stitcher.io/blog/php-enum-style-guide)
0 commit comments