Skip to content

Commit 2fd1075

Browse files
authored
Merge branch 'smknstd:main' into main
2 parents e758651 + 9d79b45 commit 2fd1075

File tree

1 file changed

+123
-3
lines changed

1 file changed

+123
-3
lines changed

README.md

Lines changed: 123 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@ This guide is not intended to teach you PHP from the ground up, but to help deve
2222
When you struggle to understand a notion, I suggest you look for answers on the following resources:
2323
- [Stitcher's blog](https://stitcher.io/blog)
2424
- [PHP.Watch](https://php.watch/versions)
25-
- [Exploring php 8.0](https://leanpub.com/exploringphp80)
25+
- [Exploring PHP 8.0](https://leanpub.com/exploringphp80)
26+
- [PHP 8 in a nutshell](https://amitmerchant.gumroad.com/l/php8-in-a-nutshell)
2627
- [PHP The Right Way](https://phptherightway.com/)
2728
- [StackOverflow](https://stackoverflow.com/questions/tagged/php)
2829

2930
### Recent PHP releases
3031

3132
| Version |Release date|
3233
|----------------------------------------------|---|
34+
| [PHP 8.2](https://www.php.net/releases/8.2/en.php) |December 2022|
3335
| [PHP 8.1](https://www.php.net/releases/8.1/en.php) |November 2021|
3436
| [PHP 8.0](https://www.php.net/releases/8.0/en.php) |November 2020|
3537
| PHP 7.4 |November 2019|
@@ -60,6 +62,7 @@ More infos on [php.net](https://www.php.net/supported-versions.php).
6062
+ [Short closures](#short-closures)
6163
+ [Match expression](#match-expression)
6264
+ [Stringable interface](#stringable-interface)
65+
+ [Enums](#enums)
6366

6467
## Notions
6568

@@ -1085,7 +1088,7 @@ When you want to merge multiple arrays, you generally use `array_merge`:
10851088
$array1 = ['baz'];
10861089
$array2 = ['foo', 'bar'];
10871090

1088-
$array3 = array_merge($array1,$array2);
1091+
$array3 = array_merge($array1, $array2);
10891092
// $array3 = ['baz', 'foo', 'bar']
10901093
```
10911094

@@ -1333,7 +1336,7 @@ $r = add(1, ...$array); // PHP Error: Unknown named parameter $d
13331336

13341337
#### External resource
13351338

1336-
- [Named arguments in depth on stitcher's blof](https://stitcher.io/blog/php-8-named-arguments)
1339+
- [Named arguments in depth on stitcher's blog](https://stitcher.io/blog/php-8-named-arguments)
13371340
- [Named Parameters on PHP.Watch](https://php.watch/versions/8.0/named-parameters)
13381341

13391342
### Short closures
@@ -1544,3 +1547,120 @@ function myFunction(string|Stringable $param): string {
15441547
return (string) $param;
15451548
}
15461549
```
1550+
1551+
### Enums
1552+
1553+
![php-version-81](https://shields.io/badge/php->=8.1-blue)
1554+
1555+
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

Comments
 (0)