Language changes aren’t always the most spectacular, but the newer things we can do with parameters in PHP are incredible! PHP 8.0 was released

Snazzier parameters in PHP 8

submited by
Style Pass
2022-01-12 18:30:05

Language changes aren’t always the most spectacular, but the newer things we can do with parameters in PHP are incredible! PHP 8.0 was released over a year ago, and 8.1 dropped last Thanksgiving. Still, these two versions represent less than 10% of sites today.

Some older projects I’ve worked on made heavy use of objects that were meant to be immutable. You could create an object and read from it at will, but changing the internal state of the object was impossible. This came in handy for video wrappers and some other WordPress-related functionality.

Unfortunately, creating readonly properties in older versions of PHP was quite wordy. You need to first mark the properties as private then create an explicit getter/setter pair for them, with the setter throwing an error.

In some cases, you could create a specific function for each property. Often, my team would fall back on PHP’s magic __set() method:

Leave a Comment