Property Promotion

Property Promotion

Less code — easier to read

  • Added in: PHP 8.0
  • What it gives you: the ability to declare class properties right in the constructor — no duplication, no boilerplate.

Why it beats the old approach

  • no need to declare properties separately and then assign them in the constructor;
  • less noise — the structure of the class is visible at a glance;
  • a perfect fit for DTOs, requests, VOs, commands, configs and so on.

In fact, this is a "data class" the PHP way — short, with no extra libraries or code generation.

A real-world example

// ❌ Before
readonly class Product {
    public string $sku;
    public string $name;
    public float $price;

    public function __construct(string $sku, string $name, float $price) {
        $this->sku = $sku;
        $this->name = $name;
        $this->price = $price;
    }
}

// ✅ Now
readonly class Product {
    public function __construct(
        public string $sku,
        public string $name,
        public float $price,
    ) {}
}

This cuts the boilerplate almost threefold, and the class becomes self-explanatory.

Where it really helps

  • DTOs in API requests/responses;
  • command objects in CQRS;
  • values that describe a single entity without behaviour (Value Objects);
  • Symfony forms, when you need to pass data through quickly.
class CreateUserCommand {
    public function __construct(
        public string $email,
        public string $password,
        public bool $isAdmin = false,
    ) {}
}

Conclusion

Property Promotion isn't just syntactic sugar, it's an official move towards a declarative style. The code looks more concise, without extra lines.

Whole series · Next: Anonymous classes →


First published in Ukrainian in the Telegram channel “Created to die” on October 15, 2025.