OverflowException
PHPERRORCommonBounds
Adding to a full container
Quick Answer
Check capacity before push/enqueue operations; throw OverflowException to signal a full container.
What this means
Thrown when an element is added to a container that has reached its maximum capacity.
Why it happens
- 1Pushing to a fixed-size stack that is already full
- 2Enqueueing to a bounded queue at max capacity
Fix
Capacity check before add
Capacity check before add
public function push(mixed $item): void {
if (count($this->items) >= $this->maxSize) {
throw new \OverflowException("Stack is full (max: {$this->maxSize})");
}
$this->items[] = $item;
}Why this works
Checking capacity before mutation ensures the error is thrown at the push site, not inside the data structure.
Code examples
Bounded queuephp
try {
$queue->enqueue($job);
} catch (\OverflowException $e) {
$backpressure->apply();
}Same error in other languages
Sources
Official documentation ↗
PHP Manual
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev