Why use PHP 8 constructor promotion?

If you’re new to PHP, you start with a convenient syntax to write your classes.

Prior to PHP 8, developers used to write properties like that:

class Post
{
public string $title;
public string $id;

public function __construct(
s…


This content originally appeared on DEV Community and was authored by jmau111⭐⭐⭐

If you're new to PHP, you start with a convenient syntax to write your classes.

Prior to PHP 8, developers used to write properties like that:

class Post
{
    public string $title;
    public string $id;

    public function __construct(
        string $title, 
        string $id
    ) {
        $this->title = $title;
        $this->id = $id;
    }
}

If it seems redundant, that because it is, but the good news is that PHP 8 removes the hassle of repeating the same lines over and over.

The new syntax

class Post
{
    public function __construct(
        public string $title, 
        public string $id
    ) {}
}

You can even add default values if you need it. It's also available in traits.

Behind the scene, PHP does the necessary assignments for you.

Using promoted properties

Just like before PHP 8:

// e.g., within the class
$this->title
$this->id

// with accessors outside if your properties are not public

What about attributes and PHP doc?

Just like before, except that you will apply that on the constructor:

class Post
{
    public function __construct(
         #[CustomAttribute]
        public string $title, 
        public string $id
    ) {}
}

When is it useful?

I would say "most of the time," but especially for value objects that contain lots of property declarations.

Of course, there are some subtleties to know for edge cases, but this syntax is pretty straighforward.

Read the full RFC here.


This content originally appeared on DEV Community and was authored by jmau111⭐⭐⭐


Print Share Comment Cite Upload Translate Updates
APA

jmau111⭐⭐⭐ | Sciencx (2023-05-07T09:49:58+00:00) Why use PHP 8 constructor promotion?. Retrieved from https://www.scien.cx/2023/05/07/why-use-php-8-constructor-promotion/

MLA
" » Why use PHP 8 constructor promotion?." jmau111⭐⭐⭐ | Sciencx - Sunday May 7, 2023, https://www.scien.cx/2023/05/07/why-use-php-8-constructor-promotion/
HARVARD
jmau111⭐⭐⭐ | Sciencx Sunday May 7, 2023 » Why use PHP 8 constructor promotion?., viewed ,<https://www.scien.cx/2023/05/07/why-use-php-8-constructor-promotion/>
VANCOUVER
jmau111⭐⭐⭐ | Sciencx - » Why use PHP 8 constructor promotion?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/05/07/why-use-php-8-constructor-promotion/
CHICAGO
" » Why use PHP 8 constructor promotion?." jmau111⭐⭐⭐ | Sciencx - Accessed . https://www.scien.cx/2023/05/07/why-use-php-8-constructor-promotion/
IEEE
" » Why use PHP 8 constructor promotion?." jmau111⭐⭐⭐ | Sciencx [Online]. Available: https://www.scien.cx/2023/05/07/why-use-php-8-constructor-promotion/. [Accessed: ]
rf:citation
» Why use PHP 8 constructor promotion? | jmau111⭐⭐⭐ | Sciencx | https://www.scien.cx/2023/05/07/why-use-php-8-constructor-promotion/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.