Efesent Solutions

Coaching and Mentoring

Member-only story

4 new features in PHP 8 that promote bad practices

--

PHP 8 just received a major new update and loads of new features were brought to this notorious programming language. In recent years, PHP has received major updates that considerably improved its performance and emphasized the use of OOP. Nonetheless, it looks like PHP is still not letting go of dubious practices. There are many, many nice features in the new version, such as the JIT compiler — which improves performance, constructor property promotion or structured metadata. Still, some of these new features promote bad practices. Below are five examples of such bad practices and how to avoid them.

Null safe operator

This operator is going to save a lot of code lines in applications that have heavy business logic. And it’s straightforward to understand: if something in a chained call is null, then evaluate everything to null. This is also a way to short circuit the whole call. It saves mental effort, lines of code, and prevents expensive calls. It’s also present in other programming languages (javascript and C# have it with short-circuiting). So when does this operator get bad?

/** PHP 7 */
$country = null;
if ($session !== null) {
$user = $session->user;
if ($user !== null) {
$address = $user->getAddress();
if ($address !== null) {
$country =…

--

--

Dan Gurgui
Dan Gurgui

Written by Dan Gurgui

Software Engineer and Technology Enthusiast

Responses (12)