fbpx

What’s new in PHP7

Haven’t you switched to PHP7 yet? If not, you should read “How Badoo saved one million dollars switching to PHP7” and consider to change your version. Moreover, remind to update at least to 5.6 version: it’s now the only minor 5.x release that will be supported and not dismissed.

schermata-2016-12-03-alle-09-52-58

http://php.net/supported-versions.php

Ok, maybe (probably) you’re not going to save one million dollars by switching PHP version, but you can still take advantage of PHP7 optimizations. Check out here some metrics presented by Rasmus Lerdorf himself during PHPDay 2016 in Verona, Italy.

Here I’m going to recap some of the new PHP7 features before switching to 7.1 ones.

Scalar type declarations

This is the main feature along with return type declarations (we’ll talk about it next) of this new major version. From now on you can integrate function declared types (which consist from php5.4 on of Class/Interface names, self, array, callable) with primitive types such as bool, float, int, string. You have two ways to use this feature: coercive (default) and strict. You can see the differences in the snippets below

You have might noticed that declare(strict_types=1); is written directly into the file we would like to have a strict type check on. If you’re wondering about a “global directive to activate this feature” I’m afraid to disappoint you: there’s not. The main reason behind this choice is due to third-party libraries: if you write a library (or use it) you want to be free to choose between week type hinting (“old one”), coercive type hinting or strict type hinting. You can read more on official RFC

Return type declarations

You can now specify a return type of a function call. Rules are the same as scalar type declarations explained above.

Null coalescing operator

Please welcome null coalescing operator:  ??

Do you recognize this?

with ?? (that is basically only syntactic sugar) you can collapse your code

Awesome, isn’t it? And you can also chain them

Spaceship operator

Helpful to compare some expression in one line

returns 0 if comparison operands are equals, -1 if second one is greater than first, 1 if first is greater than second.

Anonymous classes

If you need an object instance of a class that does not exist, you can use anonymous classes

What I would have expected from this feature is some kind of reusability of the anonymous class

But sadly this results in a

So this feature seems to be pretty useless if you look at above examples but one situation where it can be very useful is testing: you can test interfaces by providing a mock on-the-fly and without using complex thirdparty libraries.

Final thoughts

Above are listed only a subset of features of PHP7 (most interesting ones, imho). Sometimes migrating from one major version to another can be painful but if you are at 5.6 it should not be a big issue. However you can follow this guide. Last, if you need a full list of new features, you can visit this page.

Next post we’ll consider PHP7.1 changes as they seem to be very interesting.

Stay tuned!

DonCallisto
DonCallisto
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.