What is New in PHP 8.1
PHP 8.1 adds powerful modern features that improve code readability, type safety, and performance. Major highlights include native enumerations, readonly properties, fibers for concurrency, the never return type, intersection types, and many syntax improvements that make development easier and more reliable.
Key New Features
Enumerations (Enums)
Enums provide a clean way to define a set of named values with built-in type safety.
enum Suit {
case Hearts;
case Diamonds;
case Clubs;
case Spades;
}
function describe(Suit $suit): string { ... }
Readonly Properties
Properties declared as readonly can be written only once, usually in the constructor, making objects truly immutable after creation.
class Post {
public readonly string $title;
public readonly int $id;
public function __construct(string $title, int $id) {
$this->title = $title;
$this->id = $id;
}
}
First-Class Callable Syntax
A simpler way to create callables from functions or methods using the ... syntax.
$callable = strlen(...);
$method = $object->method(...);
New in Initializers
Objects and complex expressions can now be used in default values for parameters, properties, and constants.
function test(array $data = new ArrayObject()) { ... }
Pure Intersection Types
Combine multiple types that a value must satisfy using &.
function iterate(Traversable & Countable $items) { ... }
Never Return Type
Indicates a function never returns normally (it exits or throws).
function redirect(string $url): never {
header("Location: $url");
exit;
}
Final Class Constants
Class constants can be marked final to prevent overriding in child classes.
class ParentClass {
final public const VALUE = "fixed";
}
Fibers
Fibers introduce lightweight cooperative concurrency, allowing code to be paused and resumed.
They help implement advanced async patterns without complex callbacks.
Array Unpacking with String Keys
The spread operator now works with arrays that have string keys.
$array = ['a' => 1, ...$anotherArray];
Explicit Octal Notation
Octal numbers now require the 0o prefix for clarity.
$value = 0o77; // equals 63
New Functions and Features
| Area | New Items |
|---|---|
| Core | array_is_list() - checks if an array is a sequential list |
| File System | fsync(), fdatasync() |
| Attributes | #[ReturnTypeWillChange] attribute |
| Sodium | XChaCha20 encryption functions |
Backward Incompatible Changes
- Passing null to non-nullable internal parameters is now restricted.
- The Serializable interface is deprecated.
- Many resources are converted to object types (e.g., FTP\Connection, LDAP\Connection, PgSql\Connection).
- HTML entity functions handle single quotes by default.
- Restrictions on accessing $GLOBALS in certain contexts.
- MySQLi default error mode changed to exceptions.
- Implicit incompatible internal function argument types are deprecated.
Performance Improvements
PHP 8.1 includes significant optimizations across the engine:
- Inheritance cache to reduce class linking overhead.
- Faster class name resolution.
- Improved JIT support, including ARM64 backend.
- Optimizations in ext/date, SPL iterators, serialize/unserialize, and many internal functions.
- Benchmarks show notable speed gains in real-world applications like Symfony and WordPress.