Lesson 3 of 9 · 18m
match, Nullsafe Chains, and Constructor Promotion Without Cargo Cults
The syntax upgrades that remove whole classes of bugs — strict match semantics, intentional null handling, and smaller constructors.
PHP 8 syntax is often described as “quality of life,” which undersells it. These features matter because they remove ambiguity. match compares strictly, ?-> makes null propagation visible, and constructor promotion removes the copy-paste constructor where property assignments drift out of sync. Used well, they make code shorter and more explicit. Used carelessly, they just create denser legacy code.
match is strict and exhaustive by contract
$label = match ($status) {
InvoiceStatus::Draft => 'Editable',
InvoiceStatus::Issued => 'Awaiting payment',
InvoiceStatus::Paid => 'Settled',
InvoiceStatus::Void => 'Cancelled',
};match uses strict comparison, returns a value, and throws UnhandledMatchError when no arm applies. That last behavior is the one people forget — and it is valuable. Add an enum case, run your tests or static analyzer, and every incomplete match becomes a list of places that need a decision.
A default arm is appropriate for open-ended input, not for enums whose completeness you control. Silencing the exhaustiveness check with default => null recreates the old switch bug where new states disappear quietly.
nullsafe is not null avoidance
$country = $order->customer()?->address()?->country;The operator means: if the left side is null, stop and produce null. It is ideal in reads where absence is legitimate, especially views and DTO assembly. It is terrible as a substitute for deciding whether the object should have been present at all.
Compare these:
$taxRate = $order->customer()?->region()?->taxRate ?? throw new DomainException('Order has no tax jurisdiction');The first chain says “maybe absent”; the ?? throw says “absence violates the business rule.” That distinction is the design. If everything ends in ?? '' or ?? 0, you have merely moved corruption downstream.
Constructor promotion compresses, not designs
final class CreateInvoice
{
public function __construct(
private readonly CustomerRepository $customers,
private readonly Clock $clock,
) {}
}Promotion is excellent for dependency injection and small value objects. It is a warning sign when the constructor grows ten parameters: the syntax made a design problem compact enough to miss. Five collaborators is usually the point where you ask whether a service has too many responsibilities.
Small syntax, real review rules
In review, I ask three questions. Does every enum/state have a match arm? Is each ?-> documenting legitimate absence? Is this promoted constructor a cohesive object or a parameter bag? Those questions catch more trouble than arguing about formatting.
Field notes
matcharms do not fall through; you do not writebreak. Multiple conditions use commas:case A, Bhas no direct equivalent in one match arm.UnhandledMatchErrorextendsError, notException. Catch it only when translating boundary input, not as normal control flow.- The nullsafe operator short-circuits the entire chain.
$a?->b()->c()is safe even ifc()would be called on null. - Promotion cannot use
callableas a property type because PHP does not support callable properties; use a Closure type or interface instead. - Shorter code is not automatically clearer. Dense chains belong in reads, not in multi-step business operations.
Was this lesson helpful?