SPL Introduction — Standard PHP Library
debt(d5/e3/b5/t5)
Closest to 'specialist tool catches it' (d5). The detection_hints list phpstan as the tool, with automated: no, meaning misuse (e.g. using arrays instead of SplFixedArray, throwing generic Exception) won't be caught by default linting but can be flagged by phpstan with appropriate rules or custom configuration. It won't surface at compile time or via default linter passes.
Closest to 'simple parameterised fix' (e3). The quick_fix describes targeted substitutions: swap array-based stacks for SplStack/SplQueue, replace generic Exception throws with InvalidArgumentException/RuntimeException, swap file handling for SplFileObject. These are localized replacements within one component, not a one-liner but not cross-cutting either.
Closest to 'persistent productivity tax' (b5). The term applies_to all contexts (web, cli, queue-worker) and the common_mistakes show recurring patterns developers must keep in mind across collection classes, exception hierarchies, and file operations. Not using SPL interfaces when building collection classes creates ongoing interoperability debt that touches multiple work streams, but it doesn't define the system's overall architecture.
Closest to 'notable trap' (t5). The misconception field states directly: developers believe 'SPL is rarely used in modern PHP,' when in fact SPL exceptions and iterators are pervasive throughout modern frameworks. This is a well-documented gotcha that competent developers eventually learn — they underestimate SPL's relevance and fail to use standard interfaces, causing interoperability issues.
TL;DR
Explanation
SPL provides: Data structures (SplStack, SplQueue, SplDoublyLinkedList, SplMinHeap, SplMaxHeap, SplPriorityQueue, SplFixedArray — more memory-efficient than arrays). Iterators (DirectoryIterator, RecursiveDirectoryIterator, ArrayIterator, FilterIterator). Interfaces (Iterator, IteratorAggregate, Countable, ArrayAccess, Serializable). Exceptions (LogicException, RuntimeException, InvalidArgumentException etc.). File handling (SplFileInfo, SplFileObject). SplFixedArray is significantly faster than PHP arrays for integer-indexed data of known size. PHP 7+ SplDoublyLinkedList uses less memory than arrays for large datasets.
Common Misconception
Why It Matters
Common Mistakes
- Using arrays where SplFixedArray would be faster for large integer-indexed sets.
- Not throwing SPL exceptions (InvalidArgumentException, LogicException) — they're the standard.
- Not using Countable/Iterator interfaces when building collection classes.
Code Examples
// Reinventing what SPL provides:
class Stack {
private array $items = [];
public function push($item) { $this->items[] = $item; }
public function pop() { return array_pop($this->items); }
}
// Use SplStack directly:
$stack = new \SplStack();
$stack->push('first');
$stack->push('second');
echo $stack->pop(); // 'second'
// Standard exceptions:
if ($id <= 0) throw new \InvalidArgumentException('ID must be positive');