PHP 7.4 is now readily available and it’s the fourth feature update to the PHP 7 generation. It has several new features, saying goodbye to some older stuff.
PHP 7.4 brings big performance boosts and some much needed improvements to code readability. There are some important new additions. Let’s take a look.
The new Arrow functions
PHP 7.4 introduces Arrow functions, a.k.a. “short closures”. They remove the need for the “use” keyword and you can use “$this” like normal closures. You can use Arrow functions to write one-liner functions.
This is another big change to PHP 7.4 and it means that class properties now support type declarations. All types are supported, with the exception of “void” and “callable”, but you can use them with the “var” notation.
This class allows to retain a reference to an object that doesn’t prevent the object from being destroyed. It’s basically the same as “str_split”, but for multi-byte strings.
This can bring significant performance boosts, if you’re using a framework. Preloading allows the server to load the PHP files in the memory on startup and they will be permanently available. You can execute these files for any future request. A downside is that if the source of the preloaded files changes, then you have to restart the server.
Custom object serialization
This adds two new magic methods: “_serialize” and “_unserialize”. They combine the two existing mechanisms and this should make a more readable and reliable code.
Things that we say goodbye to
It’s actually a long list of stuff that is going away. Among them are:
- Nested ternary operators without explicit parentheses
- Array and string offset access using curly braces
- (real) cast and is_real() function
- Unbinding $this when $this is used
- allow_url_include INI option
- “parent” keyword without parent class
- Using array_key_exists() on objects
- Magic quotes functions
- Implode with historical parameter order
- FILTER_SANITIZE_MAGIC_QUOTES is deprecated, use FILTER_SANITIZE_ADD_SLASHES instead.
- Passing a non-string pattern to mb_ereg_replace() is deprecated.
- Passing the encoding as 3rd parameter to mb_strrpos() is deprecated.
PHP 7.4 also brings some changes by removing extensions from the core. In this case three extensions are moving to PECL and are no longer part of the PHP distribution. They are:
As expected, there are a few more changes that are coming in PHP 7.4.0. The official release notes features them in detail.