PHP 7.4 Available on All Servers

October 18, 2019

We're happy to announce the release candidate of PHP 7.4 is now available on all servers.

PHP 7.4 brings many new syntax features to the PHP language while at the same time continuing to boost the speed of PHP 7. There are also a few deprecations developers should be aware of as the PHP language is cleaned up in preparation for a future PHP 8.

Typed Properties

PHP 7.4 introduces support for property type declarations. For example:

class User {
    public int $id;
    public string $name;

    public function __construct(int $id, string $name) {
        $this->id = $id;
        $this->name = $name;
    }
}

Default values for typed properties have to match the type of the property. The only exception is that float properties also accept integer default values, consistent with the handling for parameter types. Typed properties cannot have a null default value, unless the type is explicitly nullable (?Type). This is in contrast to parameter types, where a null default value automatically implies a nullable type.

Learn more.

Null Coalescing Operator

PHP 7.4 adds the null coalescing assignment operator (??=), a shorthand for null coalescing operations (??).

Instead of writing:

$this->request->data['comments']['user_id'] = $this->request->data['comments']['user_id'] ?? 'value';

You can now write:

$this->request->data['comments']['user_id'] ??= 'value';

Learn more.

Arrow Functions

PHP 7.4's new "arrow functions" are a shorter way to write anonymous functions.

Instead of writing:

return array_map(function ($x) use ($arr) { return $arr[$x]; }, $keys);

You can now write:

return array_map(fn($x) => $arr[$x], $keys);

Learn more.

Numeric Literal Separator

PHP 7.4 follows the lead of other modern programming languages by allowing underscores as visual separators in numeric literals. Instead of writing:

const ASTRONOMICAL_UNIT = 149597870700;

You can now write:

const ASTRONOMICAL_UNIT = 149_597_870_700;

Learn more.

Spread Operator Within Arrays

Before PHP 7.4, PHP already had support for argument unpacking. Starting with PHP 7.4, you can use argument unpacking with the spread operator (...) in array expressions. For example:

$parts = ['apple', 'pear'];
$fruits = ['banana', 'orange', ...$parts, 'watermelon'];

// Result: ['banana', 'orange', 'apple', 'pear', 'watermelon'];

Learn more.

Foreign Function Interface (FFI)

To simplify the writing of PHP extensions, PHP now supports FFI which allows calling C functions and using C data types directly from PHP code. Though this feature isn't directly relevant to most PHP developers, it will be very helpful to people developing PHP extensions.

Learn more.

PHP File Preloading

For extremely high traffic PHP applications, a new low-level optimization is available with PHP 7.4: PHP file preloading. The performance benefits of file preloading over the OPCache are very, very small. If you're handling millions of dynamic requests an hour with your PHP app, you may benefit from this feature.

Learn more.

Custom Object Serialization

PHP 7.4 adds new magic methods for custom object serialization. Though custom object serialization was already possible, the new magic methods work around issues in previous approaches to object serialization.

// Returns array containing all the necessary state of the object.
public function __serialize(): array;

// Restores the object state from the given data array.
public function __unserialize(array $data): void;

Learn more.

Reflection for References

Until now, libraries needed to use hacks to detect references. PHP 7.4 adds a ReflectionReference class to allow library developers to work around this issue.

Learn more.

Weak References

Weak references allow the programmer to retain a reference to an object which does not prevent the object from being destroyed.

Learn more.

Password Hashing Registry

The new password_algos() function will list available password hashing algorithms available for use with password_hash(). This allows developers to detect which password hashing algorithms are available on the system.

print_r(password_algos());
  Array (
      [0] => "argon2i"
      [1] => "argon2id"
  )

Learn more.

Changes and Deprecations

PHP 7.4 adds deprecation warnings for syntax and functionality that will change or be forbidden in the next version of PHP which will likely be PHP 8.

See PHP's documentation on upgrading to 7.4 for a complete list of deprecations and changes.

Nesting ternary operators without explicit parentheses

Unlike essentially all other languages, the ternary operator in PHP is left-associative rather than right-associative. In order for this to be corrected in a future version of the PHP language, PHP 7.4 will now emit deprecation warnings when nested ternary operators are used without parenthesis.

// Code like this:
$a ? $b : $c ? $d : $e

// should be replaced by:
($a ? $b : $c) ? $d : $e

// or:
$a ? $b : ($c ? $d : $e)

Learn more.

Allow throwing exceptions from __toString()

Until now, throwing exceptions from __toString() was forbidden and would result in a fatal error, making it dangerous to call arbitrary code inside __toString(). This restriction was due to limitations of PHP's implementation.

Starting with PHP 7.4, you can now throw exceptions from inside __toString().

Learn more.

Concatenation Precedence

A long-standing problem with the PHP language has been the equal precedence of the '.', '+' and '-' operators. This would result in non-intuitive operator evaluation order.

Starting with PHP 7.4, '.' has lower precedence than '+' and '-'.

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

// Previous behavior: evaluated left-to-right.
echo ("sum: " . $a) + $b;

// PHP 7.4 behavior: addition and subtraction have a higher precedence.
echo "sum :" . ($a + $b);

Learn more.

Other Deprecations and Changes

  • Curly brackets no longer allowed for referencing array and string indexes. Learn more.
  • Notices are now emitted when array access syntax used on non-arrays. Learn more.
  • proc_open can now execute programs without going through a shell. Learn more.
  • strip_tags now also accepts arrays of tags. Learn more.
  • Improvements to password_hash. Learn more.
  • PEAR is deprecated as it is no longer maintained. Learn more.

Learn about additional deprecations in PHP 7.4.

App Compatibility

Most apps that are compatible with PHP 7.3 will be compatible with PHP 7.4.

Extension Compatibility

If your apps rely on PECL extensions or third-party PHP extensions such as ionCube, you'll need to wait for the extension developer to add PHP 7.4 support.

The good news is that most PECL extensions that support PHP 7.3 already support PHP 7.4. To learn about specific PECL extensions, see our PECL extension documentation which we've updated to include information on PHP 7.4 support.

How to Switch to PHP 7.4

To use PHP 7.4 with one of your apps, go to the app's Settings in ServerPilot and change the Runtime to PHP 7.4.

As always, your servers and apps using PHP 7.4 will automatically receive updates as new PHP 7.4 releases become available. When PHP 7.4.0 is released, all apps using PHP 7.4 RC will automatically begin using PHP 7.4.0.