From e73a211acd5a4c27432999f0c78249230b752015 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Sun, 16 Apr 2023 10:30:10 +0100 Subject: [PATCH] daily-email: add 2023-04-16 --- src/content/daily-email/2023-04-16.md | 39 +++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/content/daily-email/2023-04-16.md diff --git a/src/content/daily-email/2023-04-16.md b/src/content/daily-email/2023-04-16.md new file mode 100644 index 000000000..e3fbab468 --- /dev/null +++ b/src/content/daily-email/2023-04-16.md @@ -0,0 +1,39 @@ +--- +title: > + Refactoring with readonly classes in PHP 8.2 +pubDate: 2023-04-16 +permalink: > + archive/2023/04/16/refactoring-with-readonly-classes-in-php-8-2 +tags: + - php +--- + +Marian Kostadinov ([stochnagara on Twitter](https://twitter.com/stochnagara)) replied to Friday's email about DTOs and value objects to tell me about `readonly` classes, which can be done in PHP 8.2. + +Looking at the previous class: + +```php +class AccountDetails { + + public function __construct( + public readonly string $accountNumber, + public readonly string $sortCode, + ) {} + +} +``` + +Instead of setting each property as `readonly`, the whole class can instead be marked as `readonly`: + +```php +readonly class AccountDetails { + + public function __construct( + public string $accountNumber, + public string $sortCode, + ) {} + +} +``` + +Thanks for the suggestion, Marian!