
What is New in PHP 7.4?
What is New in PHP 7.4?
In our previous articles, we’ve explained what is new in every version of the PHP. Here, in this article, we will have a look at the new or refined functionality in PHP 7.4 This version will probably be released in December 2019, but the date is yet to be confirmed.
Short closures for one-liner functions
- Short closures don’t need to use the keyword to access the parent scope
- Short closures contain only one line, which is also the return statement.
array_map(function (User $user) { return $user->id; }, $users) array_map(fn(User $user) => $user->id, $users)
Preloading to improve performance
Preloading is an astonishing addition to PHP’s core, which can result in some major performance improvements.
Currently, in the framework, you need to load the files and have to be recompiled on every request. Preloading functionality allows the server to load PHP files in memory on startup, and have them permanently available to all subsequent requests.
Typed properties in classes
In PHP 7.4, The class variables can be type hinted:
class A { public string $name; public Foo $foo; }
Custom object serialization
COS adds a new way of (de)serializing objects. This adds two new methods: serialize and unserialize.
Improved type variance
It’s good to see some major improvements appearing in PHP’s core. You can use covariant return types in the new version.
class ParentType {} class ChildType extends ParentType {} class A { public function covariantReturnTypes(): ParentType { /* … */ } } class B extends A { public function covariantReturnTypes(): ChildType { /* … */ } }
… and contravariant arguments.
class A { public function contraVariantArguments(ChildType $type) { /* … */ } } class B extends A { public function contraVariantArguments(ParentType $type) { /* … */ } }
Null coalescing assignment operator as a shorthand
Instead of doing this:
$data['date'] = $data['date'] ?? new DateTime();
You can do this:
$data['date'] ??= new DateTime();
Foreign Function Interface.
FFI brings up new opportunities for extension development in PHP, in short, You can write PHP extensions in pure PHP by calling C code from userland.
It is a little bit complex so you need knowledge of C language to use this feature efficiently.
Short open tags are deprecates
The short open tag <? has been belittled and will be removed in PHP 8. The short echo tag <?= is unaffected.
Spread operator in arrays
You can use spread operator in arrays in PHP 7.4 for numerical keys.
$arrayA = [1, 2, 3]; $arrayB = [4, 5]; $result = [0, ...$arrayA, ...$arrayB, 6 ,7]; // [0, 1, 2, 3, 4, 5, 6, 7]
Backward incompatible changes
Whenever new version releases from any framework you should have a look at the full Upgrading document before upgrading new versions.
List of major backward incompatible changes
- Calling var_dump on a DateTime or DateTimeImmutableinstance will no longer leave behind accessible properties on the object.
- openssl_random_pseudo_bytes will throw an exception in error situations.
- Attempting to serialize a PDO or PDOStatement instance will generate an Exception instead of a PDOException.
- Calling get_object_vars() on an ArrayObject instance will return the properties of the ArrayObject itself and not the values of the wrapped array or object. Note that (array)casts are not affected.