Reports whether a comparator exception left the heap in a corrupted state.
The examples focus on the operation itself, including the important distinction: recoverFromCorruption resets the corruption flag to allow operations again.
What It Does
Use the examples below to observe the behavior on a small dataset before extending it to application code.
Syntax and Return Value
SplHeap::isCorrupted()
The displayed signature describes the API or identifies any correction or optional dependency. When a parameter is optional, supply it only when the task requires it.
Example 1: Basic Use
SplMaxHeap is used because the abstract SplHeap cannot be instantiated. The isCorrupted call is shown on a small, inspectable dataset.
<?php
$heap = new SplMaxHeap();
$heap->insert(4);
$heap->insert(12);
$heap->insert(7);
echo $heap->isCorrupted() ? "corrupted" : "healthy";
Output from a successful local PHP 8.4 run:
healthy
Example 2: Checking Behavior
SplMaxHeap is used because the abstract SplHeap cannot be instantiated. The isCorrupted call is shown on a small, inspectable dataset.
<?php
$heap = new SplMaxHeap();
$heap->insert(2);
$heap->insert(9);
$heap->insert(5);
$heap->insert(1);
echo $heap->isCorrupted() ? "corrupted" : "healthy";
Output from a successful local PHP 8.4 run:
healthy
Example 3: Practical Example
SplMaxHeap is used because the abstract SplHeap cannot be instantiated. The isCorrupted call is shown on a small, inspectable dataset.
<?php
$heap = new SplMaxHeap();
$heap->insert(19);
$heap->insert(3);
$heap->insert(11);
echo $heap->isCorrupted() ? "corrupted" : "healthy";
Output from a successful local PHP 8.4 run:
healthy
Important Behavior and Edge Cases
A normally operating heap returns false; it does not diagnose application-level data errors.
Comparison with Related Approaches
| Approach | Difference |
|---|---|
| SplHeap::isCorrupted | Reports whether a comparator exception left the heap in a corrupted state. |
| Related API or alternative | recoverFromCorruption resets the corruption flag to allow operations again. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Ignoring the operation-specific limits | A normally operating heap returns false; it does not diagnose application-level data errors. |
| Confusing it with a related API | recoverFromCorruption resets the corruption flag to allow operations again. |
Key Takeaways
- Reports whether a comparator exception left the heap in a corrupted state.
- A normally operating heap returns false; it does not diagnose application-level data errors.
- recoverFromCorruption resets the corruption flag to allow operations again.
Use SplHeap::isCorrupted when its documented behavior matches the problem. Test the relevant edge cases in your own runtime before deploying it.