oliverdavies.uk/content/node.098d8367-a714-4e16-97b5-44f42a2a1cc2.json

91 lines
No EOL
4.1 KiB
JSON

{
"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 <p>One of my favorite features that was introducted in PHP 8 was promoted constructor properties.<\/p>\n\n<p>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<p>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<pre><code class=\"language-php\">class AccountDetails {\n\n public function __construct(\n public string $accountNumber,\n public string $sortCode,\n ) {}\n\n}\n<\/code><\/pre>\n\n<p>Without promoted constructor properties, I'd need to create the properties and assign them manually, and I'd have this:<\/p>\n\n<pre><code class=\"language-php\">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-&gt;accountNumber = $accountNumber;\n $this-&gt;sortCode = $sortCode;\n }\n\n}\n<\/code><\/pre>\n\n<p>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 <p>One of my favorite features that was introducted in PHP 8 was promoted constructor properties.<\/p>\n\n<p>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<p>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<pre><code class=\"language-php\">class AccountDetails {\n\n public function __construct(\n public string $accountNumber,\n public string $sortCode,\n ) {}\n\n}\n<\/code><\/pre>\n\n<p>Without promoted constructor properties, I'd need to create the properties and assign them manually, and I'd have this:<\/p>\n\n<pre><code class=\"language-php\">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-&gt;accountNumber = $accountNumber;\n $this-&gt;sortCode = $sortCode;\n }\n\n}\n<\/code><\/pre>\n\n<p>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
}
]
}