php version guide
PHP 8.0 Changes
PHP 8.0 is a major compatibility boundary. Union types, attributes, named arguments, match, constructor property promotion, and the nullsafe operator are valuable, while stricter error behaviour can expose legacy assumptions.
Changes Worth Recognising
- Use union types when multiple types are genuinely part of the API.
matchuses strict comparison and does not fall through likeswitch.- Named arguments couple callers to parameter names, so library maintainers must treat names carefully.
- The nullsafe operator short-circuits a read chain; it is not a replacement for validation.
Representative Code
PHP example
<?php
function badge(string $status): string
{
return match ($status) {
'draft' => 'Needs review',
'published' => 'Live',
default => 'Unknown',
};
}
echo badge('published') . PHP_EOL;
// Prints:
// Live
Upgrade Review
- Run tests for loose string-number comparisons and internal function calls.
- Review
switchconversions carefully becausematchis strict. - Do not adopt named arguments for unstable third-party parameter names without considering compatibility.
Treat an 8.0 migration as a tested application change, not a runtime package update.
Practice
Replace a Status Switch
Convert a switch that maps order status strings to labels into a match expression. Include an explicit default branch.
Show solution
Return the match expression directly. Its strict comparisons and lack of fall-through make the mapping easier to review.