PHP 8.1 brings incredible new features, quality-of-life improvements, and a few changes that will eventually prove to be more useful.

Some of these new features are:

  • Enums or Enumerations.

Besides the fact that enumerations make you a more meticulous developer, what enums do is allow you to define a set of constants, where the value of these doesn’t matter.
Before, you had to use special strings or numbers internally to store and work with parameters, but in PHP 8.1 Enums will make the application code more readable, and avoid unexpected application states.

enum OrderStatus
{
    case Received;
    case Cancelled;
    case Refunded;
    case Shipped;
    case Completed;
}

You can also use the enum when type hinting:

public function updateOrderStatus(OrderStatus $orderStatus): JsonResponse
{
    // ...
}
  • Fibers

This feature brings lightweight and controlled concurrency to PHP. In essence, a Fiber is a code block that maintains its own stack (variables and state), they are blocks of code that can be started, paused, resumed, terminated by the main code.

Fiber by itself does not let simultaneous execution of multiple Fibers or the main thread and a Fiber (say, two Curl downloads at the same time). Be that as it may, it is a stepping stone for concurrent PHP frameworks to successfully manage their execution stack, and allow simultaneous execution.

$fiber = new Fiber(function (): void {
    echo 'Hi Boosters...' . PHP_EOL;
    Fiber::suspend();
    echo 'Hello again from the Booster side...' . PHP_EOL;
});

echo 'Starting the app...' . PHP_EOL;
$fiber->start();

echo 'Taken control back...' . PHP_EOL;

echo 'Resuming Fiber...' . PHP_EOL;
$fiber->resume();

echo 'App completed' . PHP_EOL;

The output would be:

Starting the app...
Hi Boosters...
Taken control back...
Resuming Fiber...
Hello again from the Booster side...
App completed
  • Intersection type

The first thing that needs to be cleared is the two different scenarios that happen when working with types in any language:

First: Allow properties to be typed using different types where the property must match at least one of them.

Second: Allow properties to be typed using different types where the property must match all of the types.

Now, what PHP 8.0 was able to do is solve the first scenario with union types, and what PHP 8.1 is trying to solve (at least partially) is the second scenario in the form of intersection types.

Here’s an example for better understanding:

“Consider PHP's built-in Iterator and Countable interfaces. The Iterator interface makes it possible to iterate the class object using foreach. However, unless the same class implements the Countable interface, it is not possible to call the count() function on such objects.

With an Intersection Type, it is now possible to type-check class objects to implement both Iterator and Countable interfaces.”

However, intersection types only support class and interface names as intersection members. Scalar types (array, void, mixed, callable, never, iterable, null, etc.) are not allowed.

public function countIterate(Iterator&Countable $value) {
    foreach($value as $val) {
        echo $val . PHP_EOL;
    }

    echo 'Total count: ' . count($value);
}
  • Never Return Type

This is a keyword that indicates that the method will never return anything, explicitly or implicitly. Put in other words: your method must never reach its final brace because it will either terminate the execution (die/exit) or throw an exception.

It is also a completely new return type added to PHP 8.1

function redirect(string $url): never {
    header('Location: ' . $url);
    exit();
}

IDEs that recognize the never return type might recognize the unreachable code:

redirect('<https://www.elaniin.com/>');
echo 'Redirected!'; // Unreachable/unexecuted code.
  • New Hashing Algorithms

With this feature, PHP becomes now the first programming language to include two of the fastest hashing algorithms that can easily chew through data almost at the speed of a storage media in its standard library:

  1. xxHash
  2. MurMurHash3

And besides these features PHP 8.1 will add many more and we have the list:


The information of how this new version of PHP will help developers is out there is just a matter of starting to try it out! We recommend updating your codebase as soon as possible, especially if you’re using PHP 7.4 (which is now in security updates only) or lower (because, you know, end of life 😬).