Skip to content
AgileLogics
3 min read

Constructor Property Promotion

Prerequisites: Object-oriented basics

PHP 8 lets you declare and assign properties directly in the constructor's parameter list, skipping the separate property declarations and manual $this->x = $x assignments from the previous lesson.

class User
{
    public function __construct(
        public string $name,
        public string $role = 'member',
    ) {}

    public function isAdmin(): bool
    {
        return $this->role === 'admin';
    }
}

This is functionally identical to the longer version in Object- Oriented Basics — just far less repetition, and the modern, idiomatic way to write it.