Content · Web Development · PHP

PHP Intersection and DNF Types

Intersection types require a value to satisfy every listed interface, and DNF types allow unions of intersections. Parenthesize intersections inside unions; DNF types were introduced in PHP.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

Intersection types require a value to satisfy every listed interface, and DNF types allow unions of intersections.

What It Does


Intersection types require a value to satisfy every listed interface, and DNF types allow unions of intersections. The following examples distinguish a normal use case, a boundary case and a practical application.

Requirements and Compatibility


PHP 8.2 or later for disjunctive normal form (DNF) types.

The code focuses on the core concept. Production implementation may also need routes, validation, persistence, authorization or deployment tooling.

Example 1: Basic Behavior


Start with the smallest meaningful working case.

<?php
interface CanRead { public function read(): string; }
interface CanWrite { public function write(string $text): void; }
function copyFrom(CanRead&CanWrite $source): string { return $source->read(); }
class MemoryStream implements CanRead, CanWrite { public function read(): string { return 'ok'; } public function write(string $text): void {} }
echo copyFrom(new MemoryStream());

Example 2: Another Case


Examine the edge case and the observable result.

<?php
interface Identified { public function id(): int; }
interface Named { public function name(): string; }
class Entry implements Identified, Named { public function id(): int { return 7; } public function name(): string { return 'A'; } }
function display((Identified&Named)|string $item): string { return is_string($item) ? $item : $item->name(); }
echo display(new Entry());

Practical Example: Putting It to Use


Apply the technique to a small practical task.

<?php
interface Paged { public function page(): int; }
interface Sortable { public function sort(): string; }
class Query implements Paged, Sortable { public function page(): int { return 2; } public function sort(): string { return 'name'; } }
function describe(Paged&Sortable $query): string { return $query->page() . ':' . $query->sort(); }
echo describe(new Query());

Important Behavior and Edge Cases


Parenthesize intersections inside unions; DNF types were introduced in PHP 8.2.

Comparison with Related Functions or Approaches


OptionWhat it means in practice
Intersection and DNF TypesIntersection types require a value to satisfy every listed interface, and DNF types allow unions of intersections.
Related approachUnion types accept any of several alternatives, not all simultaneously.

Common Beginner Mistakes


MistakeCorrect approach
Ignoring the important boundary for Intersection and DNF TypesParenthesize intersections inside unions; DNF types were introduced in PHP 8.2.
Assuming a related mechanism has the same behaviorUnion types accept any of several alternatives, not all simultaneously.

Key Takeaways


  • Intersection types require a value to satisfy every listed interface, and DNF types allow unions of intersections.
  • Parenthesize intersections inside unions; DNF types were introduced in PHP 8.2.
  • Union types accept any of several alternatives, not all simultaneously.

Before adapting these examples, verify the return values, test the failure paths and confirm that your application meets its security and environment requirements.

Share this resourceSend this page to someone who may find it useful.