What PHP 8 Brings: Attributes, Match Expressions, Union Types & More
This article reviews PHP 8’s most impactful new features—including first‑class attributes, constructor property promotion, match expressions, union types, and named arguments—showing code examples and explaining how they modernize PHP development.
PHP 8’s official release on 2020‑11‑26 brings a set of stable, groundbreaking features. Although this article was written while PHP 8 was still in alpha, it summarizes the most important additions for developers.
Attributes
Attributes are a major addition to PHP, allowing metadata to be attached to functions, parameters, classes, methods, constants, and properties. Previously simulated via docblocks, they are now first‑class citizens that can be accessed through reflection.
@@Route(Http::POST, '/store/123/item')
class ItemCreateHandler {
public function __invoke() {
// ...
}
}Some developers, such as Derick Rethans, prefer the Rust‑style syntax #[FooAttribute] for defining attributes.
Object‑Oriented Updates (Constructor Property Promotion)
Traditional PHP classes required explicit property declarations and a constructor to assign them:
class Response {
private int $code;
private string $body;
private array $headers;
public function __construct(int $code, string $body, array $headers) {
$this->code = $code;
$this->body = $body;
$this->headers = $headers;
}
}PHP 8 allows a more concise syntax using constructor property promotion:
class Response {
public function __construct(
private int $code,
private string $body,
private array $headers,
) {}
}Match Expressions
Match expressions provide a shorter, expression‑based alternative to switch statements, with implicit breaks and the ability to return values directly.
$y = match ($x) {
1 => 3,
2 => 4,
3, 4 => 5,
// ...
default => throw new \RuntimeException('Not happening, bud'),
};Union Types
PHP 8 introduces union types, allowing a parameter or return type to accept multiple types, such as int|float. This reduces the need for extensive phpdoc annotations.
class Number {
private int|float $number;
public function setNumber(int|float $number): void {
$this->number = $number;
}
public function getNumber(): int|float {
return $this->number;
}
}Nullable types can be expressed as Product|null or using the shorthand ?Product:
public function handle(Product|null $product): int;
// equals
public function handle(?Product $product): int;Named Arguments
Named arguments let developers pass values based on parameter names rather than position, improving readability:
// Positional arguments
json_encode($data, 0, 512);
// Named arguments
json_encode(value: $data, options: 0, depth: 512);Conclusion
PHP 8 continues to evolve, and the community welcomes further improvements. These new features collectively modernize PHP development and promise a more expressive, concise coding experience.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
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.
