PHP 8 - try out all new features

Published on

PHP 8 - try out all new features

PHP 8 is already in it's release candidate stage, with RC 3 being released on October 29th, and the general availability release targeted for November 26th. So it is time to take a look at all the new and upcoming features of PHP 8. You can take a look at PHP 8's release schedule here.

UPDATE: Free video course If you're more of a visual learner, check out my entirely free video course, covering all new features in PHP 8.

Every feature that you see in this blogpost comes with an interactive embedded editor, where you can modify the PHP code and evaluate the results yourself.

The official upgrade guide can be found on GitHub.

Editing and evaluating the code requires cross-site cookies. If you have those disabled, you can click "Edit on Laravel Playground" to jump into the code examples and edit them.

New Features

Added support for union types (RFC)

A union type accepts values of multiple different types, rather than a single one.

Added WeakMap (RFC)

Weak maps allow creating a map from objects to arbitrary values (similar to SplObjectStorage) without preventing the objects that are used as keys from being garbage collected. If an object key is garbage collected, it will simply be removed from the map.

This is a very useful new feature, as it allows users to worry even less about leaving memory leaks in their code. While this might not be an issue for most PHP developers, it is certainly something to watch out for when writing long-running processes, for example using ReactPHP. With WeakMaps, references to an object automatically get garbage collected, once the object is no longer available.

If you would do the same thing with an array, you would still have a reference to the object, thus leaking memory.

New ValueError Exception

PHP 8 introduces a new built-in exception class called ValueError. It extends \Exception and PHP throws this exception, every time you pass a value to a function, which has a valid type - but can not be used for the operation. Prior to PHP 8, this would result in a warning.

Here are some examples:

Allowing variadic argument when overriding functions

Any number of function parameters may now be replaced by a variadic argument, as long as the types are compatible. For example, the following code is now allowed:

Static return type (RFC)

The static return type may now be used in PHP 8 to identify that a method returns the class, that this method was actually called on - even if it was inherited (late static binding).

Class name literal on object (RFC)

It is now possible to fetch the class name of an object using $object::class. The result is the same as get_class($object).

Variable Syntax Tweaks (RFC)

New and instanceof can now be used with arbitrary expressions, using new (expression)(...$args) and $obj instanceof (expression).

Stringable interface (RFC)

PHP 8 introduces a new Stringable interfaces, which gets automatically added, once a class implements the __toString method. You do not need to explicitly implement this interface.

Traits can now define abstract private methods (RFC)

throw can now be used as an expression (RFC)

The throw statement can now be used in places where only expressions are allowed, like arrow functions, the coalesce operator and the ternary/elvis operator.

An optional trailing comma is now allowed in parameter lists (RFC)

Similar to the trailing comma in arrays, you can now define a trailing comma in a parameter list.

Catching exceptions without storing in a variable (RFC)

It is now possible to write catch (Exception) to catch an exception without storing it in a variable.

Added support for mixed type (RFC)

PHP 8 introduces a new type called mixed that you can use. A type of mixed would be equivalent to array|bool|callable|int|float|null|object|resource|string.

Added support for Attributes

PHP 8's attributes actually consist of a number of RFCs:

Attributes are definitely one of the biggest changes in PHP 8, and they can be a bit tricky to understand at first. In short, attributes allow you to add meta-data to PHP functions, parameters, classes, etc. This meta-data can then be retrieved programatically - whereas in PHP 7 and lower, you would need to parse doclocks, attributes allow you to access this information deeply integrated into PHP itself.

To make this a bit clearer, lets say that you want to allow users to add a middleware to a controller class/method by using an attribute.

Added support for constructor property promotion (RFC)

This RFC proposes to introduce a short hand syntax, which allows combining the definition of properties and the constructor:

Added support for match expression. (RFC)

This RFC proposes adding a new match expression that is similar to switch but with safer semantics and the ability to return values.

Added support for nullsafe operator (?->) (RFC)

When the left hand side of the operator evaluates to null the execution of the entire chain will stop and evalute to null. When it is not null it will behave exactly like the normal -> operator.

Added support for named arguments (RFC)

Named arguments allow passing arguments to a function based on the parameter name, rather than the parameter position. This makes the meaning of the argument self-documenting, makes the arguments order-independent, and allows skipping default values arbitrarily.