From 9fe5a268bbd698f657a08cf96657c0d3c7235f60 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Fri, 28 Feb 2025 00:33:37 +0000 Subject: [PATCH] Add daily email for 2025-02-13 readonly --- source/_daily_emails/2025-02-13.md | 61 ++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 source/_daily_emails/2025-02-13.md diff --git a/source/_daily_emails/2025-02-13.md b/source/_daily_emails/2025-02-13.md new file mode 100644 index 00000000..d36de672 --- /dev/null +++ b/source/_daily_emails/2025-02-13.md @@ -0,0 +1,61 @@ +--- +title: Using readonly in PHP +date: 2025-02-13 +permalink: daily/2025/02/13/readonly +tags: + - software-development + - php +cta: ~ +snippet: | + Did you know you can make classes and properties read-only since PHP 8.1? +--- + +Since PHP 8.1, properties on PHP classes have been able to be marked as `readonly`. + +Once they've been set, they can't be changed or overwritten. + +This is great for data transfer objects (DTOs) that you want to be immutable and know the values are the same as when they were set. + +Here's an example: + +```php +class MyDto { + + public function __construct( + public readonly string $name, + public readonly int $age, + ) { + } + +} +``` + +I don't need to make the properties private or protected and write getter methods for them. + +I can make them public, knowing the values can't be changed. + +## readonly classes + +Since PHP 8.2, the entire class can be `readonly`, making the code cleaner and easier to read. + +This class does the same as the previous example: + +```php +readonly class MyDto { + + public function __construct( + public string $name, + public int $age, + ) { + } + +} +``` + +## Here's the thing + +This is a great example of how the PHP language continues to evolve and improve, and it's been a great way to make it easier to keep my code clean and with fewer bugs. + +If you want a way to easily add `readonly` to properties or classes, [take a look at Rector][0] which can automate it for you. + +[0]: {{site.url}}/daily/2025/01/31/rector