{ "uuid": [ { "value": "70c71a01-1c5a-4b25-8706-d02a4284662e" } ], "langcode": [ { "value": "en" } ], "type": [ { "target_id": "daily_email", "target_type": "node_type", "target_uuid": "8bde1f2f-eef9-4f2d-ae9c-96921f8193d7" } ], "revision_timestamp": [ { "value": "2025-05-11T09:00:01+00:00" } ], "revision_uid": [ { "target_type": "user", "target_uuid": "b8966985-d4b2-42a7-a319-2e94ccfbb849" } ], "revision_log": [], "status": [ { "value": true } ], "uid": [ { "target_type": "user", "target_uuid": "b8966985-d4b2-42a7-a319-2e94ccfbb849" } ], "title": [ { "value": "Using readonly in PHP" } ], "created": [ { "value": "2025-02-13T00:00:00+00:00" } ], "changed": [ { "value": "2025-05-11T09:00:01+00:00" } ], "promote": [ { "value": false } ], "sticky": [ { "value": false } ], "default_langcode": [ { "value": true } ], "revision_translation_affected": [ { "value": true } ], "path": [ { "alias": "\/daily\/2025\/02\/13\/readonly", "langcode": "en" } ], "body": [ { "value": "\n

Since PHP 8.1, properties on PHP classes have been able to be marked as readonly<\/code>.<\/p>\n\n

Once they've been set, they can't be changed or overwritten.<\/p>\n\n

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.<\/p>\n\n

Here's an example:<\/p>\n\n

class MyDto {\n\n  public function __construct(\n    public readonly string $name,\n    public readonly int $age,\n  ) {\n  }\n\n}\n<\/code><\/pre>\n\n

I don't need to make the properties private or protected and write getter methods for them.<\/p>\n\n

I can make them public, knowing the values can't be changed.<\/p>\n\n

readonly classes<\/h2>\n\n

Since PHP 8.2, the entire class can be readonly<\/code>, making the code cleaner and easier to read.<\/p>\n\n

This class does the same as the previous example:<\/p>\n\n

readonly class MyDto {\n\n  public function __construct(\n    public string $name,\n    public int $age,\n  ) {\n  }\n\n}\n<\/code><\/pre>\n\n

Here's the thing<\/h2>\n\n

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.<\/p>\n\n

If you want a way to easily add readonly<\/code> to properties or classes, take a look at Rector<\/a> which can automate it for you.<\/p>\n\n ", "format": "full_html", "processed": "\n

Since PHP 8.1, properties on PHP classes have been able to be marked as readonly<\/code>.<\/p>\n\n

Once they've been set, they can't be changed or overwritten.<\/p>\n\n

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.<\/p>\n\n

Here's an example:<\/p>\n\n

class MyDto {\n\n  public function __construct(\n    public readonly string $name,\n    public readonly int $age,\n  ) {\n  }\n\n}\n<\/code><\/pre>\n\n

I don't need to make the properties private or protected and write getter methods for them.<\/p>\n\n

I can make them public, knowing the values can't be changed.<\/p>\n\n

readonly classes<\/h2>\n\n

Since PHP 8.2, the entire class can be readonly<\/code>, making the code cleaner and easier to read.<\/p>\n\n

This class does the same as the previous example:<\/p>\n\n

readonly class MyDto {\n\n  public function __construct(\n    public string $name,\n    public int $age,\n  ) {\n  }\n\n}\n<\/code><\/pre>\n\n

Here's the thing<\/h2>\n\n

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.<\/p>\n\n

If you want a way to easily add readonly<\/code> to properties or classes, take a look at Rector<\/a> which can automate it for you.<\/p>\n\n ", "summary": null } ] }