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 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: |
One of my favorite features that was introducted in PHP 8 was promoted constructor properties.
If I'm passing arguments into a constructor, I can declare a visibility and it will be promoted to a property on the class.
Here's an example of a value of a data transfer object that accepts a sort code and account number as strings:
class AccountDetails {
public function __construct(
public string $accountNumber,
public string $sortCode,
) {}
}
Without promoted constructor properties, I'd need to create the properties and assign them manually, and I'd have this:
class AccountDetails {
public string $accountNumber;
public string $sortCode;
public function __construct(
string $accountNumber,
string $sortCode,
) {
$this->accountNumber = $accountNumber;
$this->sortCode = $sortCode;
}
}
Whilst text editors and IDEs can create the properties automatically, I prefer this as it's less code, more readable and easier to understand.
format: full_html processed: |One of my favorite features that was introducted in PHP 8 was promoted constructor properties.
If I'm passing arguments into a constructor, I can declare a visibility and it will be promoted to a property on the class.
Here's an example of a value of a data transfer object that accepts a sort code and account number as strings:
class AccountDetails {
public function __construct(
public string $accountNumber,
public string $sortCode,
) {}
}
Without promoted constructor properties, I'd need to create the properties and assign them manually, and I'd have this:
class AccountDetails {
public string $accountNumber;
public string $sortCode;
public function __construct(
string $accountNumber,
string $sortCode,
) {
$this->accountNumber = $accountNumber;
$this->sortCode = $sortCode;
}
}
Whilst text editors and IDEs can create the properties automatically, I prefer this as it's less code, more readable and easier to understand.
summary: null field_daily_email_cta: { }