On this page
PHP 8+ Features
PHP 8.0 Highlights
Named Arguments
function createUser(string $name, string $email, bool $active = true) { /* ... */ }
createUser(email: '[email protected]', name: 'Alice');
Constructor Property Promotion
class User {
public function __construct(
public string $name,
public string $email,
private int $id = 0,
) {}
}
Match Expression
$result = match ($status) {
'draft' => 'Draft',
'published' => 'Published',
default => 'Unknown',
};
Union and Mixed Types
function process(int|string $id): array|null { /* ... */ }
Attributes (Annotations)
#[Route('/api/users', methods: ['GET'])]
class UserController { /* ... */ }
Nullsafe Operator
$country = $user?->getAddress()?->getCountry();
PHP 8.1
Enums
enum Status: string {
case Draft = 'draft';
case Published = 'published';
public function label(): string {
return match($this) {
self::Draft => 'Draft',
self::Published => 'Published',
};
}
}
Readonly Properties
class Point {
public function __construct(
public readonly float $x,
public readonly float $y,
) {}
}
First-class Callable Syntax
$fn = strlen(...);
echo $fn('hello'); // 5
PHP 8.2
Readonly Classes
readonly class Config {
public function __construct(
public string $host,
public int $port,
) {}
}
Disjunctive Normal Form (DNF) Types
function process((A&B)|null $input): void { /* ... */ }
PHP 8.3
Typed Class Constants
class Api {
public const string VERSION = '1.0';
}
json_validate()
if (json_validate($jsonString)) {
$data = json_decode($jsonString, true);
}
Fibers (PHP 8.1)
Lightweight concurrency for I/O-bound tasks:
$fiber = new Fiber(function (): void {
$result = someAsyncOperation();
Fiber::suspend($result);
});
$fiber->start();
Frameworks like Revolt and Amp build on fibers for async PHP.
Migration Tips
- Run PHPStan or Psalm for static analysis before upgrading
- Test with the new version in CI
- Check deprecated features in the PHP migration guide
- Enable strict types:
declare(strict_types=1);
Modern PHP is a capable, typed language — these features bring it closer to Java and TypeScript in expressiveness.