Skip to content

Commit 152351e

Browse files
author
Petr Pánek
authored
Small hint for PHP 8 (#204)
1 parent 8e8d563 commit 152351e

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

book/ar-single-table-inheritance.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,21 @@ property.
192192
];
193193
}
194194
```
195+
196+
197+
Using PHP 8 syntax
198+
-------------------------
199+
In PHP 8 we can simplify `instantiate()` method with new match expressions:
200+
201+
```php
202+
public static function instantiate($row)
203+
{
204+
return match ($row['type']) {
205+
SportCar::TYPE => new SportCar(),
206+
HeavyCar::TYPE => new HeavyCar(),
207+
default => new self
208+
};
209+
}
210+
```
211+
212+
If `type` is not a string as in this example but is an integer, it is necessary to cast `$row['type']` to `int`, beacuse match expressions use strict comparison. In this case replace `$row['type']` with `(int)$row['type']`.

0 commit comments

Comments
 (0)