# New features

PHP 7.4 comes with a remarkable amount of new features. We'll start with a list of all new features, and then look at changes and deprecations.

A note before we dive in though: if you're still on a lower version of PHP, you'll also want to read what's new in PHP 7.3.

# Arrow functions rfc

Arrow functions, also called "short closures", allow for less verbose one-liner functions.

While you'd previously write this:

array_map(function (User $user) { 
    return $user->id; 
}, $users)

You can now write this:

array_map(fn (User $user) => $user->id, $users)

There are a few notes about arrow functions:

You can read about them in depth here.

# Typed properties rfc

Class variables can be type hinted:

class A
{
    public string $name;
    
    public ?Foo $foo;
}

There's lots to tell about this feature, so I wrote a dedicated post about them.

# Improved type variance rfc

I also wrote about PHP's type system in the past, so it's good to see some improvements are actually arriving in PHP's core.

Type variance is another topic worth its own blog post, but in short: you'll be able use covariant return types –

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 rfc

Next is the null coalescing assignment operator, a shorthand for null coalescing operations. Instead of doing this:

$data['date'] = $data['date'] ?? new DateTime();

You can do this:

$data['date'] ??= new DateTime();

# Array spread operator RFC

Next up, it's now possible to use the spread operator in arrays:

$arrayA = [1, 2, 3];

$arrayB = [4, 5];

$result = [0, ...$arrayA, ...$arrayB, 6 ,7];

// [0, 1, 2, 3, 4, 5, 6, 7]

Note that this only works with arrays with numerical keys.

# Numeric Literal Separator RFC

PHP 7.4 allows for underscores to be used to visually separate numeric values. It looks like this:

$unformattedNumber = 107925284.88;

$formattedNumber = 107_925_284.88;

The underscores are simply ignored by the engine.

# Foreign function interface rfc

Moving on to some more core-level features: foreign function interface or "FFI" in short, allows us to call C code from userland. This means that PHP extensions could be written in pure PHP and loaded via composer.

It should be noted though that this is a complex topic. You still need C knowledge to be able to properly use this feature.

# Preloading rfc

Another lower-level feature is preloading. It's is an amazing addition to PHP's core, which can result in some significant performance improvements.

In short: if you're using a framework, its files have to be loaded and linked on every request. Preloading allows the server to load PHP files in memory on startup, and have them permanently available to all subsequent requests.

The performance gain comes of course with a cost: if the source of preloaded files are changed, the server has to be restarted.

Do you want to know more? I wrote a dedicated post about preloading here.

# Custom object serialization rfc

Two new magic methods have been added: __serialize and __unserialize. The difference between these methods and __sleep and __wakeup is discussed in the RFC.

# Reflection for references rfc

Libraries like Symfony's var dumper rely heavily on the reflection API to reliably dump a variable. Previously it wasn't possible to properly reflect references, resulting in these libraries relying on hacks to detect them.

PHP 7.4 adds the ReflectionReference class which solves this issue.

# Weak references rfc

Weak references are references to objects, which don't prevent them from being destroyed.

# mb_str_split added RFC

This function provides the same functionality as str_split, but on multi-byte strings.

# Password Hashing Registry RFC

Internal changes have been made to how hashing libraries are used, so that it's easier for userland to use them.

More specifically, a new function password_algos has been added which returns a list of all registered password algorithms.

# Changes and deprecations

Besides new features, there are also lots of changes to the language. Most of these changes are non-breaking, though some might have an effect on your code bases.

Note that deprecation warnings aren't per definition "breaking", but merely a notice to the developer that functionality will be removed or changed in the future. It would be good not to ignore deprecation warnings, and to fix them right away; as it will make the upgrade path for PHP 8.0 more easy.

# Left-associative ternary operator deprecation RFC

The ternary operator has some weird quirks in PHP. This RFC adds a deprecation warning for nested ternary statements. In PHP 8, this deprecation will be converted to a compile time error.

1 ? 2 : 3 ? 4 : 5;   // deprecated
(1 ? 2 : 3) ? 4 : 5; // ok

# Exceptions allowed in __toString RFC

Previously, exceptions could not be thrown in __toString. They were prohibited because of a workaround for some old core error handling mechanisms, but Nikita pointed out that this "solution" didn't actually solve the problem it tried to address.

This behaviour is now changed, and exceptions can be thrown from __toString.

# Concatenation precedence RFC

If you'd write something like this:

echo "sum: " . $a + $b;

PHP would previously interpret it like this:

echo ("sum: " . $a) + $b;

PHP 8 will make it so that it's interpreted like this:

echo "sum :" . ($a + $b);

PHP 7.4 adds a deprecation warning when encountering an unparenthesized expression containing a . before a + or - sign.

# array_merge without arguments UPGRADING

Since the addition of the spread operator, there might be cases where you'd want to use array_merge like so:

$merged = array_merge(...$arrayOfArrays);

To support the edge case where $arrayOfArrays would be empty, both array_merge and array_merge_recursive now allow an empty parameter list. An empty array will be returned if no input was passed.

# Curly brackets for array and string access RFC

It was possible to access arrays and string offsets using curly brackets:

$array{1};
$string{3};

This has been deprecated.

# Invalid array access notices RFC

If you were to use the array access syntax on, say, an integer; PHP would previously return null. As of PHP 7.4, a notice will be emitted.

$i = 1;

$i[0]; // Notice

# proc_open improvements UPGRADING

Changes were made to proc_open so that it can execute programs without going through a shell. This is done by passing an array instead of a string for the command.

# strip_tags also accepts arrays UPGRADING

You used to only be able to strip multiple tags like so:

strip_tags($string, '<a><p>')

PHP 7.4 also allows the use of an array:

strip_tags($string, ['a', 'p'])

# ext-hash always enabled rfc

This extension is now permanently available in all PHP installations.

# Improvements to password_hash rfc

This is a small change and adds argon2i and argon2id hashing support when PHP was compiled without libargon.

# PEAR not enabled by default EXTERNALS

Because PEAR isn't actively maintained anymore, the core team decided to remove its default installation with PHP 7.4.

# Several small deprecations RFC

This RFC bundles lots of small deprecations, each with their own vote. Be sure to read a more detailed explanation on the RFC page, though here's a list of deprecated things:

# Other changes UPGRADING

You should always take a look at the full UPGRADING document when upgrading PHP versions.

Here are some changes highlighted:

# RFC voting process improvements

This is technically not an update related to PHP 7.4, though it's worth mentioning: the voting rules for RFC's have been changed.

# Hang on now…

If you've made it this far, chances are you are interested in PHP. Maybe you're also interested in reading my other content? Here are some of the things I wrote.

Oh and lastly, follow me on Twitter if you want to stay up to date!