What’s new in PHP7.1


If you have not read yet our blog post about new features in PHP7, we highly recommend to do so before continue reading.

In this post we’re going to analyze new features proposed by PHP7.1 that have “seen the light” 2 October 2017.

Nullable types

Function’s argument type and return type can be marked as null. You can declare types as nullable by prefixing a question mark ? before argument/return type.

Speaking of argument type we would like to highlight the differences (and similarities) between the following two methods

With this feature you can distinguish between mandatory argument that can be null (foo function) and optional ones (bar function). I’ve tried to think about this difference and how it enhances language strength. What I figured out can be resumed in two points:

1. No difference for your API (method) internals

Before using $argument you probably need to check its type in one case or the other, so you don’t get any advantage in using one form or other. However it’s true that API must “communicate” something to those who use it.

2. Semantics

It’s – in a kind of way – a consequence of last sentence at point one, explained above. For a client could be important to know when an attribute it’s mandatory and can be null or when it is simply optional.

Speaking now about returning null, our only suggestion is to NEVER return null from your functions. When you return null from a function you can run into this

Because sometimes you simply forget to check return type.

Basically you’re creating extra-checking work for yourself: you need to implement ad-hoc error handling, your code quickly become dirty, possibly your method name (that should have no lack of intent) should be modified as getBarOrNull and so on

One solution is to use Null Object pattern. For instance if you’re returning an array, return an empty array instead of  null.

Another solution is to throw an exception that can either be caught immediately or go up through calling stack. Exceptions have more semantics than a general “Uncaught Error” and can lead to a cleaner code.

Void functions

If your function has only side effects probably you’ll never return anything from your function. From PHP7.1 you can take advantage of this behavior – that is pretty common in other programming languages – specifying void as function return type

Symmetric array destructuring

You probably know list() syntax that allows you to “consume” array elements in one statement

With PHP7.1 you can use this equivalent (but more readable to me) syntax

However in PHP <= 7 this “destruction” would have worked only with non-associative array with ordered and consecutive keys

PHP7.1 offers a syntax to destruct even associative arrays or ones that does not have consecutive keys

Class constant visibility

Now you can specify the visibility for your constants like you do with variables (private, protected, public)

String negative offset

It can be possible to use negative index to access strings and with string manipulation functions


If you need the whole list of new features you can visit PHP official manual page

Articoli: 21

Lascia una risposta

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *

Questo sito usa Akismet per ridurre lo spam. Scopri come i tuoi dati vengono elaborati.