One place I avoid constructor property promotion

Just to keep the intent clear

Joel Clermont
Joel Clermont

One of my favorite PHP 8 features is constructor property promotion. It cleans up a lot of boilerplate code and makes classes easier to read.

But one place I won't use this handy feature is if I need to possibly override the value inside the constructor. For example, maybe the constructor allows a null value to be passed in, but I might initialize it to a different value.

In those cases, I explicitly declare the class property and set it in the constructor, just like the pre-PHP 8 days. It's a small change, but it seems clearer to me.

Here to help,


P.S. If you have found this newsletter helpful and want to show your support, click the "Toss a coin" link below and give whatever you like. I really appreciate it!

Toss a coin in the jar if you found this helpful.
Want a tip like this in your inbox every weekday? Sign up below 👇🏼

Level up your Laravel skills!

Each 2-minute email has real-world advice you can use.