haphpiness

These are things in PHP which make me genuinely_happy();

Constructor Property Promotion

PHP 8.0 eliminated the most tedious boilerplate in the language: declaring a property, listing it as a constructor parameter, and assigning one to the other. Three places to maintain the same information, reduced to one.

// Before: say the same thing three times
class User {
    private string $name;
    private string $email;
    private int $age;
    private bool $active;

    public function __construct(string $name, string $email, int $age, bool $active = true) {
        $this->name = $name;
        $this->email = $email;
        $this->age = $age;
        $this->active = $active;
    }
}

// After: say it once
class User {
    public function __construct(
        private string $name,
        private string $email,
        private int $age,
        private bool $active = true,
    ) {}
}

// Combined with readonly (PHP 8.1) — immutable value objects in one line:
class Point {
    public function __construct(
        public readonly float $x,
        public readonly float $y,
    ) {}
}

You can mix promoted and non-promoted parameters freely. Promoted properties support all visibility modifiers and the readonly flag. It's one of those features that, once you use it, you can never go back.

Significance: Developer Experience

Boilerplate isn't just annoying — it's a source of bugs (forget to assign one property, misspell a name, mismatch types). Constructor promotion eliminates the boilerplate entirely, making simple value objects and DTOs a joy to define.