102 lines
2.8 KiB
YAML
102 lines
2.8 KiB
YAML
|
uuid:
|
||
|
- value: 9d0e577d-eca2-4b05-9266-00f94901ae69
|
||
|
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: |
|
||
|
Refactoring with readonly classes in PHP 8.2
|
||
|
created:
|
||
|
- value: '2023-04-16T00: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/16/refactoring-with-readonly-classes-in-php-8-2
|
||
|
langcode: en
|
||
|
body:
|
||
|
- value: |
|
||
|
<p>Marian Kostadinov (<a href="https://twitter.com/stochnagara">stochnagara on Twitter</a>) replied to Friday's email about DTOs and value objects to tell me about <code>readonly</code> classes, which can be done in PHP 8.2.</p>
|
||
|
|
||
|
<p>Looking at the previous class:</p>
|
||
|
|
||
|
<pre><code class="language-php">class AccountDetails {
|
||
|
|
||
|
public function __construct(
|
||
|
public readonly string $accountNumber,
|
||
|
public readonly string $sortCode,
|
||
|
) {}
|
||
|
|
||
|
}
|
||
|
</code></pre>
|
||
|
|
||
|
<p>Instead of setting each property as <code>readonly</code>, the whole class can instead be marked as <code>readonly</code>:</p>
|
||
|
|
||
|
<pre><code class="language-php">readonly class AccountDetails {
|
||
|
|
||
|
public function __construct(
|
||
|
public string $accountNumber,
|
||
|
public string $sortCode,
|
||
|
) {}
|
||
|
|
||
|
}
|
||
|
</code></pre>
|
||
|
|
||
|
<p>Thanks for the suggestion, Marian!</p>
|
||
|
|
||
|
|
||
|
format: full_html
|
||
|
processed: |
|
||
|
<p>Marian Kostadinov (<a href="https://twitter.com/stochnagara">stochnagara on Twitter</a>) replied to Friday's email about DTOs and value objects to tell me about <code>readonly</code> classes, which can be done in PHP 8.2.</p>
|
||
|
|
||
|
<p>Looking at the previous class:</p>
|
||
|
|
||
|
<pre><code class="language-php">class AccountDetails {
|
||
|
|
||
|
public function __construct(
|
||
|
public readonly string $accountNumber,
|
||
|
public readonly string $sortCode,
|
||
|
) {}
|
||
|
|
||
|
}
|
||
|
</code></pre>
|
||
|
|
||
|
<p>Instead of setting each property as <code>readonly</code>, the whole class can instead be marked as <code>readonly</code>:</p>
|
||
|
|
||
|
<pre><code class="language-php">readonly class AccountDetails {
|
||
|
|
||
|
public function __construct(
|
||
|
public string $accountNumber,
|
||
|
public string $sortCode,
|
||
|
) {}
|
||
|
|
||
|
}
|
||
|
</code></pre>
|
||
|
|
||
|
<p>Thanks for the suggestion, Marian!</p>
|
||
|
|
||
|
|
||
|
summary: null
|
||
|
field_daily_email_cta: { }
|