PHP 8.1 made me delete these 30 lines of code

The less code you write, the less you have to maintain

Italo Baeza Cabrera
3 min readAug 20, 2021

PHP 8.1 is just around the corner, around a bunch of RFCs that got approved for the sake of making the developer write less, which is always a good way to accelerate your time writing and reading.

I had a part on a project that was working for PHP 7.3. The class on question is basically a “Delivery Route” handler. The delivery man selects one, is saved in the session, and then is retrieved for every thing that needs to interact with the route: items, detours, chauffeurs, you name it.

Theoretically, using the same code on PHP 8.1 will make no difference. This is just part of the code.

What it does? It sets an active delivery route for a person driving a truck, which will later be used by a session helper to retrieve it. It’s just one of many parts, but this was hit almost in every HTTP request.

The problem is that I have to hide the $user property and $activeRoute properties behind a protected keyword, and offer methods to retrieve them. The developer can’t change the user, otherwise it breaks things downward the spiral.

After checking what two of the new RFC are coming for PHP 8.1, along some that where already approved in prior versions, it allowed me to slim that class into a few lines. Note that, since PHP 7.4 will be no more, I can just jump into the bandwagon of the new version.

What the hell just happened? There are two key features that are introduced in PHP 8.1.

Read-only properties

One of the most useful features of PHP 8.1 is protecting the write of a property. Before, when you made a property “public”, anyone could just swap it, even accidentally, and break a class.

The new readonly keyword makes the property non-writable. An developer can still change the user name, but not the property.

// This is allowed
$handler->user->name = 'John Doe';
// This is NOT allowed
$handler->user = new User;

Thanks to the above, I no longer need to use a function, only access the property directly since I know for sure that nothing will change it.

Initializing with “new"

Optional arguments just got slightly buffed. The most common pattern is to add default values as strings, integers, floats, or null, and just then initialize it in the class constructor. This is very common, no sin committed.

PHP 8.1 will allow the new expression to initialize an object instance as default. If you need complex initializing, .like by calling a function, this won’t change anything for you, but for simple new classes, it will.

// This is allowed
public function __construct(
public User $user = new User('John')
)
// This is NOT allowed
public function __construct(
public User $user = User::make('John')
}

There are more things incoming for PHP 8.1, specially Fibers, so I’m very curious on how the PHP community will receive these new additions.

--

--

Italo Baeza Cabrera

Graphic Designer graduate. Full Stack Web Developer. Retired Tech & Gaming Editor. https://italobc.com