PHP 7.2 is scheduled to release on November 30. I can’t remember, before the release of PHP 7.0 in December of 2015, anticipating new versions of a programming language. PHP 7 made a few really big leaps – nearly doubling its processing speed, for one thing – and the trend continues in 7.1 and now 7.2.
Here are a couple of my favorites:
New (Smooth) Operators
In 7.0 and 7.1 we got a couple new operators: the null coalesce and the spaceship. In 7.2 we’re going to get the null-coalesce-equals (??=) assignment operator. I can’t count how many times I’ve wished for that one.
null-coalesce-equals means “If the left parameter is null, assign the value of the right parameter to the left one. If the left value is not null, do nothing”. So, for example, instead of
$this->request->data['comments']['user_id'] = $this->request->data['comments']['user_id'] ?? 'value';
we can conditionally assign as follows:
$this->request->data['comments']['user_id'] ??= 'value';
So much cleaner!
Even Better Type Hinting
PHP is a loosely typed language. That never bothered me until a couple years ago when I was involved with a project that had a huge codebase. Most of the errors we saw in production were caused by the wrong type of data being accidentally passed into or returned from one function or another. I think I lost plenty of my already thinning hair trying to figure out where those errant data types were coming from.
Starting with 7.0, we got much more powerful type hinting. We even got the option to turn on strict data typing on a file-by-file basis. Before 7.0 we were only able to type hint for arrays, classes and interfaces. 7.0 added basic data types (so we could specify integers, doubles, strings, etc.) and the ability to require return types. 7.1 added the capacity to hint for
null-or-type and to specify
null-or-type and void return types.
In 7.2, we’re finally getting the capacity to hint a generic
object type. Up to now we had to hint a specific class or interface. If you wanted to pass in a generic object, you had to create your own
object interface and then have all your objects implement it. Aside from implementing a completely superfluous interface in every other class being a royal pain in the ass, your code wouldn’t work with framework objects that didn’t also implement the interface.
This is kinda-sorta related to type hinting. Up to now, if you type hinted a method in a parent class, all inheriting classes also had to implement that type hint. Starting with 7.2, we can override (or even leave out) the type hint in a child class method.
Also, we can now override abstract methods. Boo-ya!
Other Good Stuff
- Argon2i is now standard for hashing.
$extra_headersarray – about time!
count()lets you know when you’re trying to count something that’s not
Looks like Christmas is coming a month early this year!