We will look at an addition to PHP’s syntax that gets rid of much unnecessary boilerplate code.
Let’s take a look on how you would define a DTO and build onto that:
This was before PHP8.0, where you had to write each property’s name four times… Thanks to constructor property promotion, we can rewrite the above code like this:
This is a BIG difference, now let’s look at this feature in-depth and see what is actually going on:
The Basic Idea
Ditch the class properties and the variable assignments, and prefix constructor parameter with privacy accessibility like public, protected and private. PHP will take that new syntax and transform it to normal syntax behind the scenes at runtime before executing code.
So when we write our code, PHP will transform it just like so: