Intersection Type Error
Fatal error: Uncaught TypeError: App\Service::process(): Argument #1 ($handler) must be of type Countable&Iterator, App\SimpleList givenQuick Answer
The argument does not implement all interfaces required by the intersection type. Ensure the class implements every interface listed in the intersection type.
Why This Happens
PHP 8.1 intersection types require a value to satisfy all specified types simultaneously. If a class implements Countable but not Iterator, it fails the Countable&Iterator type check. The class must implement all interfaces in the intersection.
The Problem
function process(Countable&Iterator $handler): void {
// ...
}
class SimpleList implements Countable {
public function count(): int { return 0; }
}
process(new SimpleList()); // Missing IteratorThe Fix
function process(Countable&Iterator $handler): void {
// ...
}
class SimpleList implements Countable, Iterator {
private array $items = [];
public function count(): int { return count($this->items); }
public function current(): mixed { return current($this->items); }
public function key(): mixed { return key($this->items); }
public function next(): void { next($this->items); }
public function rewind(): void { reset($this->items); }
public function valid(): bool { return key($this->items) !== null; }
}Step-by-Step Fix
- 1
Read the type requirements
The error message lists all interfaces required by the intersection type using the & separator.
- 2
Check the class implements list
Look at the class definition to see which interfaces it implements and which are missing.
- 3
Implement missing interfaces
Add the missing interface to the class implements list and implement all required methods.
Bugsly catches this automatically
Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.
Try Bugsly free