Get "PHP 8 in a Nuthshell" (Now with PHP 8.4)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

Constructor Property Promotion in PHP 8

Wouldn’t it be nice if you don’t have to declare the class property over and over again just to use it across the class? Currently, you’d do it by first declaring it…

  • In the property declaration.
  • In the constructor parameters.
  • In the property assignment in the costructor body.

So, a class with properties declared would look like so.

<?php

class Book
{
    private string $name;
    private string $author;

    public function __construct(
        string $name = 'The Alchemist', 
        string $author = 'Paulo Coelho'
    ) {
        $this->name = $name;
        $this->author = $author;
    }
}

Things are getting interesting in PHP 8 for this scenario. According to this RFC (which is accepted), Constructor Property Promotion is coming in PHP 8.

Constructor Property Promotion

With the use of constructor property promotion, it will be really easy to declare class properties by just declaring and defining properties all in one place.

Let’s modify the previous example to see how it would look like when using constructor property promotion.

<?php

class Book
{
    public function __construct(
        private string $name = 'The Alchemist', 
        private string $author = 'Paulo Coelho'
    ) {}
}

As you can see, using construstor property promotion, there are couple of benefits at our disposal.

  • There’s no need to declare properties in the class and defining them into the constructor body.
  • The syntax is more shorter and less error-prone as you don’t have to maintain same property at three different places!

Here, when a method parameter is prefixed with one of the visibility keywords public, protected or private, it is considered to be “promoted”. When a parameter is promoted, a property with the same name will be added, and a forwarding assignment to that property included in the body of the constructor.

Rules on using constructor property promotion

There are some places where you won’t be able to use constructor property promotion.

  • It can only be occurred in class constructors and not in other class methods.
  • It can’t be occurred in non-class based methods.
  • It can’t be occurred in abstract class constructors or in interfaces.
  • Properties with callable types are not eligible to be promoted.
  • If the property is nullable, nullability must be explicitly declared using ? like so.
public function __construct(public ?Type $prop = null) {}
class Test {
    // Error: Variadic parameter.
    public function __construct(public string ...$strings) {}
}
  • Promoted and non-promoted paramters can be used simultaneouly. So, following is absolutely valid.
<?php

private string $description;

class Book
{
    public function __construct(
        private string $name = 'The Alchemist', 
        private string $author = 'Paulo Coelho',
        string $description = 'A fine book'
    ) {
        $this->desdescription = $description;
    }
}

You can learn more about this feature over here.

More in PHP 8

Learn the fundamentals of PHP 8 (including 8.1, 8.2, 8.3, and 8.4), the latest version of PHP, and how to use it today with my new book PHP 8 in a Nutshell. It's a no-fluff and easy-to-digest guide to the latest features and nitty-gritty details of PHP 8. So, if you're looking for a quick and easy way to PHP 8, this is the book for you.

Like this article?

Buy me a coffee

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?