Although the new version of PHP is minor, it already carries many new, without exaggeration, cool features for both the syntax of the language and its performance. The list of innovations is not final, but the main changes have already been made and adopted. Release is scheduled for December 2019.

Key changes to the upcoming version:
')
- Typed class properties
- Preload for performance improvements
- Arrow functions for short recording of anonymous functions
- The assignment operator of the union with null (?? =)
- Covariance / contravariance in inherited method signatures
- Interface external functions, opening up new opportunities for the development of extensions for PHP
- Unpacking operator in arrays
Read more about these and other changes under the cat.
Disclaimer: Already several times in my discussions with colleagues featured an article by Brent " New in PHP 7.4 ". At first I wanted to make a translation, but in the process I understood that not all the latest updates are indicated in the text and there are some inaccuracies, so this article appeared instead of the translation.Arrow functions ( RFC )
Arrow functions allow you to make a shorter record of anonymous functions:
array_map(function (User $user) { return $user->id; }, $users) array_map(fn(User $user) => $user->id, $users)
Some features of the adopted implementation of the switch functions:
- They can access the
parent
area, so there is no need to use the use keyword. $this
also available, as in the usual anonymous functions.- Arrow functions can contain only one string, which is also the operator of the return value.
You can read more about them in this
article on Habré.
Typed Properties ( RFC )
Hooray! Class properties can now have type hint. This is a very welcome change since PHP 7 towards a stricter language typing. Now we have all the basic features for strong typing. All types are available for typing, except for
void
and
callable.
class Bar { public string $name; public ?int $amount; public Foo $foo; }
If you want me to consider in more detail the new possibilities of typing properties of objects, check in the comments and I will write a separate article about them!
A null join operator ( RFC )
Instead of such a long record:
$data['date'] = $data['date'] ?? new DateTime();
Now you can write like this:
$data['date'] ??= new DateTime();
Unpacking Operator in Arrays ( RFC )
Now you can use the unpacking operator in arrays:
$arrayA = [1, 2, 3]; $arrayB = [4, 5]; $result = [0, ...$arrayA, ...$arrayB, 6 ,7]; <i>// [0, 1, 2, 3, 4, 5, 6, 7]</i>
Note that this only works with non-associative arrays.
External Function Interface ( RFC )
The external function interface (FFI) allows you to write C code directly in PHP code. This means that PHP extensions can be written in pure PHP.
It should be noted that this is a complex topic. You still need the knowledge of C to properly use these features.
Preload ( RFC )
Preloading is a terrific addition to the PHP core, which should lead to some significant performance improvements.
For example, if you are using the framework, its files must be downloaded and recompiled for each request. When using opcache, when these files are first used in the processing of a request and then every time they are successfully checked for changes. Preloading allows the server to load the specified PHP files into memory at startup and have them permanently available for all subsequent requests, without performing additional checks for file changes.
The performance increase is “non-free” - if the preloaded files change, the server must be restarted.
Covariance / contravariance in inherited method signatures ( RFC )
Currently, PHP has mostly invariant parameter types and invariant return types. This change allows you to change the type of the parameter to one of its supertypes. In turn, the return type can be replaced by its subtype. Thus, this change will allow more strictly follow the principle of Barbara Liskov's substitution.
An example of using a covariant return type:
interface Factory { function make(): object; } class UserFactory implements Factory { function make(): User; }
and contravariant argument:
interface Concatable { function concat(Iterator $input); } class Collection implements Concatable { function concat(iterable $input) {/* . . . */} }
Custom Object Serialization ( RFC )
Two new magic methods become available:
__serialize
and
__unserialize
. This serialization mechanism combines the versatility of the
Serializable
interface with the implementation approach of
__sleep/__wakeup
methods. More details on their differences can be found in the RFC.
Priority for Concatenation Operations ( RFC )
If you wrote something like this:
echo "sum: " . $a + $b;
Now PHP would interpret it as:
echo ("sum: " . $a) + $b;
PHP 8 will interpret this differently:
echo "sum :" . ($a + $b);
PHP 7.4 adds a deprecation warning when it detects an expression containing "." before "+" or "-" and uncirculated with brackets.
Exception support in __toString
( RFC )
Previously, exceptions could not be thrown from the
__toString
magic method. The rationale for this behavior is that the conversion of objects to strings is performed in many functions of the standard library, and not all of them are ready to "correctly" handle exceptions. As part of this RFC, a comprehensive audit of string conversions in the code base was performed, and this restriction can now be removed, which was done.
Reflection for reference ( RFC )
Libraries such as
symfony/var-dumper
rely heavily on
ReflectionAPI
for accurate variable output. Previously, there was no proper support for link reflection, which made these libraries rely on hacks to find links. PHP 7.4 adds a
ReflectionReference
class that solves this problem.
Added mb_str_split
( RFC ) method
This function provides the same functionality as
str_split
, but for strings written in multibyte encodings.
Always available ext-hash
extension ( RFC )
This extension is now permanently available in all PHP installations.
PEAR is not enabled by default ( EXTERNALS )
PEAR is no longer actively maintained, the core team decided to remove it from the default installation with PHP 7.4.
Register of Password Hashing Algorithms ( RFC )
Added new function
password_algos
, which returns a list of all registered password hashing algorithms.
Weak Links ( RFC )
Weak links allow you to save a link to an object that does not prevent the destruction of this object. For example, they are useful for implementing cache-like structures.
Numeric Literal Separator ( RFC )
The lack of visual delimiters in groups of numbers increased the time for reading and debugging code, and could lead to unintended errors. Support for the underscore character in numeric literals has now been added to visually separate groups of numbers.
1_000_000_000 // int 6.674_083e-11; // float 299_792_458; // decimal 0xCAFE_F00D; // hexadecimal 0b0101_1111; // binary 0137_041; // octal
Short opening tags are deprecated ( RFC )
Short opening tag
<?
out of date and will be removed in PHP 8. The short
<?= (echo)
tag was not affected.
The left-associative ternary operator is deprecated ( RFC )
The ternary operator has some weird quirks in PHP. This RFC declares obsolete nested ternary operators.
1 ? 2 : 3 ? 4 : 5; // deprecated (1 ? 2 : 3) ? 4 : 5; // ok
In PHP 8, such a record will result in a compilation level error.
Backward incompatible changes ( UPGRADING )
Here are some of the most important at the moment back incompatible changes:
- A call to
parent::
in a non-parent class is deprecated. - Calling
var_dump
on a DateTime
or DateTimeImmutable
instance no longer makes object properties available. openssl_random_pseudo_bytes
will throw an exception in error situations caused by the OpenSSL library. Previously, it returned false, which could lead to the generation of an empty string.- An attempt to serialize a
PDO
or PDOStatement
instance generates an Exception
instead of a PDOException
. - Calling
get_object_vars()
on an ArrayObject
instance ArrayObject
return the properties of the ArrayObject
itself, not the value of the wrapped array. To get the values ​​of the wrapped array as before - ArrayObject
to type array
.