New Features in PHP 8.1
The article outlines PHP 8.1’s major enhancements—including enums, readonly properties, advanced callable syntax, fibers, intersection types, never return type, final class constants, octal literals, and performance improvements—providing code comparisons that illustrate the new syntax and behavior.
On November 25, 2021 the PHP development team released PHP 8.1, introducing a range of new language features and performance improvements.
Enums
Previously developers used class constants to represent a fixed set of values. PHP 8.1 adds native enum support.
class Status{
const DRAFT = 'draft';
const PUBLISHED = 'published';
const ARCHIVED = 'archived';
}
function acceptStatus(string $status) {...} enum Status{
case Draft;
case Published;
case Archived;
}
function acceptStatus(Status $status) {...}Readonly Properties
Readonly properties can be assigned only once, making them ideal for value objects and DTOs.
class BlogData{
private Status $status;
public function __construct(Status $status){
$this->status = $status;
}
public function getStatus(): Status{
return $this->status;
}
} class BlogData{
public readonly Status $status;
public function __construct(Status $status){
$this->status = $status;
}
}Advanced Callable Syntax
PHP 8.1 allows any function to be referenced directly using the new callable syntax.
$foo = $this->foo(...);
$fn = strlen(...);Initializer Updates
Objects can now be used as default parameter values, static variables, global constants, and property defaults.
class Service{
private Logger $logger;
public function __construct(?Logger $logger = null){
$this->logger = $logger ?? new NullLogger();
}
} class Service{
private Logger $logger;
public function __construct(Logger $logger = new NullLogger()){
$this->logger = $logger;
}
}Intersection Types
When a value must satisfy multiple type constraints, intersection types can be used.
function count_and_iterate(Iterator $value) {
if (!($value instanceof Countable)) {
throw new TypeError('value must be Countable');
}
foreach ($value as $val) { echo $val; }
count($value);
} function count_and_iterate(Iterator&Countable $value) {
foreach ($value as $val) { echo $val; }
count($value);
}Never Return Type
Functions declared with the never return type never return a value; they end execution via exit, die, or an exception.
function redirect(string $uri): never {
header('Location: ' . $uri);
exit;
}
function redirectToLoginPage(): never {
redirect('/login');
echo 'Hello'; // dead code detected by static analysis
}Final Class Constants
Final class constants can now be declared to prevent overriding in subclasses.
class Foo{
final public const XX = "foo";
}
class Bar extends Foo{
public const XX = "bar"; // Fatal error
}Explicit Octal Number Syntax
Developers can now use the 0o prefix for octal literals.
0o16 === 14; // trueFibers
Fibers provide a primitive for lightweight cooperative concurrency, allowing code blocks to be paused and resumed.
$response = $httpClient->request('https://example.com/');
print json_decode($response->getBody()->buffer())['code'];String-key Array Unpacking
Array unpacking now works with string keys as well as integer keys.
$arrayA = ['a' => 1];
$arrayB = ['b' => 2];
$result = ['a' => 0, ...$arrayA, ...$arrayB]; // ['a' => 1, 'b' => 2]Performance Improvements
Benchmarks show a 23 % speedup for the Symfony Demo app and a 3.5 % boost for WordPress, thanks to JIT backend for ARM64, inheritance caching, faster class name resolution, and numerous internal function optimizations.
New Classes, Interfaces, and Functions
New #[ReturnTypeWillChange] attribute
New fsync and fdatasync functions
New array_is_list function
New Sodium XChaCha20 functions
Deprecations and Backward Compatibility Changes
Passing null to non‑nullable internal parameters is discouraged
Tentative return types in built‑in class methods
Serializable interface is deprecated
HTML entity functions now replace single quotes by default
Various resource‑to‑object migrations (e.g., MySQLi, IMAP, FTP, GD, LDAP, PostgreSQL, Pspell)
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.