Constructor Property Promotion Shakes Up the PHP World

Nemanja Milenkovic
2 min readApr 8, 2023

--

Intro: The Setup

Ladies and gentlemen, boys and girls, developers of all ages! I present to you a magnificent innovation in the world of PHP. No longer shall we toil over the redundancy of declaring and initializing properties. Instead, allow me to introduce you to the Constructor Property Promotion feature in PHP 8.0! 🎩🐇

As a PHP developer with 20 years of experience, I have seen the rise and fall of trends, the emergence of new features, and the continuous growth of our beloved language. So, without further ado, let’s dive into this groundbreaking feature that will undoubtedly bring a smile to your face.

Act 1: The Olden Days of Constructors

Remember the days when we had to type so much just to define a class, its properties, and the constructor? Oh, the agony! It was like carrying your groceries home without bags — a balancing act of utter annoyance. Take a look at this example:

class Pizza {
private string $topping;
private int $size;

public function __construct(string $topping, int $size) {
$this->topping = $topping;
$this->size = $size;
}
}

How tedious! If only there was a way to reduce this redundancy… Wait a second, there is! Enter PHP 8.0!

Act 2: A Star is Born — Constructor Property Promotion

PHP 8.0 saw the birth of a game-changing feature: Constructor Property Promotion. With this fantastic addition, we can now define, initialize, and promote class properties in one fell swoop. No more juggling properties like a circus clown! 🤡

Here’s the same class, revamped using property promotion:

class Pizza {
public function __construct(private string $topping, private int $size) {}
}

That’s it! A single line in the constructor that does it all. We’ve saved ourselves from the excruciating pain of typing those extra lines. It’s like magic — just a wave of the PHP 8.0 wand and POOF! 🌟

Act 3: Practical Magic

Now, let’s say you’re ordering pizza, and you want to know the topping and size. Easy-peasy with constructor property promotion! Here’s how you do it:

class Pizza {
public function __construct(private string $topping, private int $size) {}

public function getTopping(): string {
return $this->topping;
}
public function getSize(): int {
return $this->size;
}
}

$myPizza = new Pizza(“Pepperoni”, 12);
echo $myPizza->getTopping(); // Pepperoni
echo $myPizza->getSize(); // 12

Epilogue: A Future of Convenience

This PHP 8.0 feature has undoubtedly saved us time, effort, and most importantly, keystrokes. Our fingers will forever be grateful. Constructor property promotion not only streamlines our code but also injects a bit of fun into our development lives.

So, dear developers, enjoy the magical world of PHP 8.0 and its Constructor Property Promotion feature. And remember, with great power comes great responsibility — use it wisely! 🧙‍♂️

Curtain closes,applause,subscribes, etc… :)

--

--

Nemanja Milenkovic

Web dev expert with 20+ yrs experience. LAMP technologies maestro, adept in PHP, Laravel, Symfony...