1. Overview
In this article, we’ll learn about the new and interesting features in PHP 7.2
Release 7.2 of PHP was done in November, 2018. It included mostly core language, security and performance improvemnts .
We’ll talk about these one by one.
You can also have a look on PHP RFC page for all the changes in PHP for current version and as well as future versions.
2. Language Core Improvements
2.1 Generic argument type declarations
Since PHP 5, we may declare the type of arguments of functions. In PHP 7.2, it’s a way more than declaring the type – now object inheritance is supported in argument type declaration. Now, with this change you can declare a function and accept any generic object.
Consider:
class Mars{ } class Earth{ } function print(object $planet){ } $earth = new Earth(); $mars = new Mars(); print($earth); print($mars);
In this code print function expects $planet variable of type object. And as Earth is-a object and Mars is-a object. We can pass objects of these class as well to print function.
2.2 Object return type declarations
With this new feature in PHP 7.2, we can declare the return type as objects.
Let’s have a look:
class Mars{ } class Earth{ } function getPlanet($switch) : object { if($switch == 'earth'){ return new Earth(); }else{ return new Mars(); } }
In this code, getPlanet declares the return type as object and can return any object at the run time.
2.3 Trailing commas in list syntax
We can leave an extra comma at the end of last item of arrays. Now we can use it in more ways:
// Grouped namepaces use Foo\Bar\{ Foo, Bar, Baz, }; // Arrays (already possible) $array = [1, 2, 3,]; // Function/method arguments (call) fooCall($arg1, $arg2, $arg3,); class Foo implements // Interface implementations on a class FooInterface, BarInterface, BazInterface, { // Trait implementations on a class use FooTrait, BarTrait, BazTrait, ; // Class member lists const A = 1010, B = 1021, C = 1032, D = 1043, ; protected $a = 'foo', $b = 'bar', $c = 'baz', ; private $blah, ; // Function/method arguments (declaration) function something(FooBarBazInterface $in, FooBarBazInterface $out,) : bool { } } // Inheriting variables from the parent scope in anonymous functions $foo = function ($bar) use ( $a, $b, $c, ) { // . . . };
2.4 Function parameter type widening in inheritance
Till now, we were forced to have the same type of parameter of a particular function in whole of the inheritance ladder but with the latest release of PHP it’s gone. We can now declare type-widened parameters of functions. Let’s see:
class SolarSystem { public function getBody(Planet $body){ } } class PlanetSystem extends SolarSystem { public function getBody(object $body){ } }
3. Security Improvements
3.1 Argon2 in password functions
Currently the most widely used algorithm for password hashing is Bcrypt. Now we have more secure alternative to it – Argon2. Argon2 is one of the most secure password hashing algorithm and was selected at the winner at Password Hashing Competition. It takes 3 cost factors for hash and it’s predecessor Bcrypt uses only 2 cost factors.
password_hash('somepassword', PASSWORD_ARGON2I, ['memory_cost' => 2048, 'time_cost' => 4, 'threads' => 3]); //Works for both bcrypt and Argon2 if(password_verify($user_password, $stored_hash)) { // password validated }
3.2 Libsodium as part of PHP Core
Libsodium is a very widely used cross platform cryptography library. It supports many security features and functions like encryption and decryption of data, digital signatures, password hashing and much more. You can familiarize yourself with Libsodium by their official documentation.
4. Notable deprecations
New releases of PHP adds new features and also deprecates some of the features of language in lieu of better alternatives and methods. You can see the complete list of deprecation.
5. Performance Improvements
According to official PHP benchmarks, PHP 7.2 can serve twice the number of requests in almost half of the latency. These performance improvements are very impressive. You must try it for your platform.
6. Conclusion
In this article, we are briefly discussing some interesting new features in PHP 7.2
There are many other changes that you can learn
But, the information illustrated in this article is a good starting point for exploring and learning about some of these new features.
Finally, all the source code for the article is available over on GitHub.