Union types and intersection types express different type constraints in PHP.
What It Does
Union types and intersection types express different type constraints in PHP. The following examples distinguish a normal use case, a boundary case and a practical application.
Requirements and Compatibility
PHP 8.x and a local test environment. Apply input validation, correct error handling and the requirements of any optional libraries.
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
A union accepts a reader or writer, unlike an intersection requiring both.
<?php
interface Reads { public function read(): string; }
interface Writes { public function write(string $value): void; }
class LogFile implements Reads, Writes { public function read(): string { return 'log'; } public function write(string $value): void {} }
function describe(Reads|Writes $obj): string { return $obj instanceof Reads ? $obj->read() : 'write-only'; }
echo describe(new LogFile());
Important Behavior and Edge Cases
A union broadens accepted values; an intersection requires multiple capabilities at once.
Comparison with Related Functions or Approaches
| Option | What it means in practice |
|---|---|
| Intersection Types vs Union Types | Union types and intersection types express different type constraints in PHP. |
| Related approach | Use small interfaces rather than unrelated large classes. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Ignoring the important boundary for Intersection Types vs Union Types | A union broadens accepted values; an intersection requires multiple capabilities at once. |
| Assuming a related mechanism has the same behavior | Use small interfaces rather than unrelated large classes. |
Key Takeaways
- Union types and intersection types express different type constraints in PHP.
- A union broadens accepted values; an intersection requires multiple capabilities at once.
- Use small interfaces rather than unrelated large classes.
Before adapting these examples, verify the return values, test the failure paths and confirm that your application meets its security and environment requirements.