{ "uuid": [ { "value": "098d8367-a714-4e16-97b5-44f42a2a1cc2" } ], "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:44+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": "Cleaner PHP code with promoted constructor properties\n" } ], "created": [ { "value": "2023-04-12T00:00:00+00:00" } ], "changed": [ { "value": "2025-05-11T09:00:44+00:00" } ], "promote": [ { "value": false } ], "sticky": [ { "value": false } ], "default_langcode": [ { "value": true } ], "revision_translation_affected": [ { "value": true } ], "path": [ { "alias": "\/daily\/2023\/04\/12\/cleaner-php-code-with-promoted-constructor-properties", "langcode": "en" } ], "body": [ { "value": "\n

One of my favorite features that was introducted in PHP 8 was promoted constructor properties.<\/p>\n\n

If I'm passing arguments into a constructor, I can declare a visibility and it will be promoted to a property on the class.<\/p>\n\n

Here's an example of a value of a data transfer object that accepts a sort code and account number as strings:<\/p>\n\n

class AccountDetails {\n\n  public function __construct(\n    public string $accountNumber,\n    public string $sortCode,\n  ) {}\n\n}\n<\/code><\/pre>\n\n

Without promoted constructor properties, I'd need to create the properties and assign them manually, and I'd have this:<\/p>\n\n

class AccountDetails {\n\n  public string $accountNumber;\n\n  public string $sortCode;\n\n  public function __construct(\n    string $accountNumber,\n    string $sortCode,\n  ) {\n    $this->accountNumber = $accountNumber;\n    $this->sortCode = $sortCode;\n  }\n\n}\n<\/code><\/pre>\n\n

Whilst text editors and IDEs can create the properties automatically, I prefer this as it's less code, more readable and easier to understand.<\/p>\n\n ", "format": "full_html", "processed": "\n

One of my favorite features that was introducted in PHP 8 was promoted constructor properties.<\/p>\n\n

If I'm passing arguments into a constructor, I can declare a visibility and it will be promoted to a property on the class.<\/p>\n\n

Here's an example of a value of a data transfer object that accepts a sort code and account number as strings:<\/p>\n\n

class AccountDetails {\n\n  public function __construct(\n    public string $accountNumber,\n    public string $sortCode,\n  ) {}\n\n}\n<\/code><\/pre>\n\n

Without promoted constructor properties, I'd need to create the properties and assign them manually, and I'd have this:<\/p>\n\n

class AccountDetails {\n\n  public string $accountNumber;\n\n  public string $sortCode;\n\n  public function __construct(\n    string $accountNumber,\n    string $sortCode,\n  ) {\n    $this->accountNumber = $accountNumber;\n    $this->sortCode = $sortCode;\n  }\n\n}\n<\/code><\/pre>\n\n

Whilst text editors and IDEs can create the properties automatically, I prefer this as it's less code, more readable and easier to understand.<\/p>\n\n ", "summary": null } ], "feeds_item": [ { "imported": "1970-01-01T00:33:45+00:00", "guid": null, "hash": "6e7b86e1b92a964e048c10b4a1bb65da", "target_type": "feeds_feed", "target_uuid": "90c85284-7ca8-4074-9178-97ff8384fe76" } ] }