Property Promotion

← Property Promotion

Ashterix

docs: English JsonRpcBundle docs, REST and MCP Bundle sections, package cards

en/articles/from-tg/series/php-can-do/property-promotion.md new file +70 βˆ’0

1 + # Property Promotion
2 +
3 + *Less code β€” easier to read*
4 +
5 + - **Added in:** PHP 8.0
6 + - **What it gives you:** the ability to declare class properties right in the constructor β€” no duplication, no boilerplate.
7 +
8 + ## Why it beats the old approach
9 +
10 + - no need to declare properties separately and then assign them in the constructor;
11 + - less noise β€” the structure of the class is visible at a glance;
12 + - a perfect fit for DTOs, requests, VOs, commands, configs and so on.
13 +
14 + In fact, this is a "data class" the PHP way β€” short, with no extra libraries or code generation.
15 +
16 + ## A real-world example
17 +
18 + ```php
19 + // ❌ Before
20 + readonly class Product {
21 + public string $sku;
22 + public string $name;
23 + public float $price;
24 +
25 + public function __construct(string $sku, string $name, float $price) {
26 + $this->sku = $sku;
27 + $this->name = $name;
28 + $this->price = $price;
29 + }
30 + }
31 +
32 + // βœ… Now
33 + readonly class Product {
34 + public function __construct(
35 + public string $sku,
36 + public string $name,
37 + public float $price,
38 + ) {}
39 + }
40 + ```
41 +
42 + This cuts the boilerplate almost threefold, and the class becomes self-explanatory.
43 +
44 + ## Where it really helps
45 +
46 + - DTOs in API requests/responses;
47 + - command objects in CQRS;
48 + - values that describe a single entity without behaviour (Value Objects);
49 + - Symfony forms, when you need to pass data through quickly.
50 +
51 + ```php
52 + class CreateUserCommand {
53 + public function __construct(
54 + public string $email,
55 + public string $password,
56 + public bool $isAdmin = false,
57 + ) {}
58 + }
59 + ```
60 +
61 + ## Conclusion
62 +
63 + > Property Promotion isn't just syntactic sugar, it's an official move towards a declarative style.
64 + > The code looks more concise, without extra lines.
65 +
66 + [Whole series](/articles/from-tg/series/php-can-do) Β· [Next: Anonymous classes β†’](/articles/from-tg/series/php-can-do/anonymous-classes)
67 +
68 + ---
69 +
70 + *First published in Ukrainian in the Telegram channel [β€œCreated to die”](https://t.me/php_while_true/70) on October 15, 2025.*