SPL Data Structures
debt(d7/e3/b3/t5)
Closest to 'only careful code review or runtime testing' (d7). The detection_hints note tools Blackfire and PHPStan, but automated detection is explicitly 'no'. PHPStan cannot reliably flag array usage as a missed SPL opportunity; Blackfire can surface O(n) performance hotspots at runtime but only after profiling under realistic load. The code_pattern (array_shift used as a queue, repeated sorting for priority) is semantic, not syntactic — it requires a developer to recognise the algorithmic intent mismatch during review or profiling, not a routine lint pass.
Closest to 'simple parameterised fix' (e3). The quick_fix describes swapping PHP array operations for SplStack, SplQueue, or SplMinHeap equivalents. This is a localised refactor — replacing push/pop/shift calls and the data structure declaration within one component or algorithm. It touches the usage site and possibly a few call sites but is not cross-cutting. Slightly above e1 because the API differs (SplStack uses push/pop/top rather than array_push/array_pop) and iteration behaviour differs, requiring care.
Closest to 'localised tax' (b3). The choice to use or omit SPL structures is scoped to specific algorithmic components (a queue worker, a heap-based scheduler). It does not reshape the broader codebase — most PHP code never touches SPL. The tax is paid only by the component that chose the wrong structure, and fixing it is confined to that component. It does not have gravitational pull across other modules.
Closest to 'notable trap' (t5). The misconception is explicitly stated: developers assume PHP arrays are always the best choice for stacks and queues, not realising SPL structures provide O(1) end-operations and lower memory for large collections. This is a documented gotcha that experienced PHP developers eventually learn, but it contradicts the intuition that 'PHP arrays do everything'. It is not catastrophic (arrays work correctly, just less efficiently) and the issue is well-documented, placing it at t5 rather than t7.
Also Known As
TL;DR
Explanation
The SPL (Standard PHP Library) data structures offer typed, memory-efficient alternatives to plain arrays for specific use cases. SplStack (LIFO), SplQueue (FIFO), and SplDoublyLinkedList avoid the overhead of shifting/reindexing arrays. SplMinHeap and SplMaxHeap implement priority queues in O(log n). SplFixedArray is a memory-efficient fixed-size integer-indexed array. SplObjectStorage maps objects to data without string keys. In practice, PHP arrays cover most needs, but SPL structures shine in algorithms, job schedulers, and data processing pipelines where their complexity guarantees matter.
Common Misconception
Why It Matters
Common Mistakes
- Using a PHP array as a stack with array_push/array_pop when SplStack expresses the intent explicitly.
- Not using SplFixedArray for large numeric arrays — it uses significantly less memory than a dynamic array.
- Using SplDoublyLinkedList when SplStack or SplQueue (which extend it) communicate intent better.
- Not considering that SplMinHeap/SplMaxHeap provide O(log n) priority queue operations vs O(n log n) for sorting an array repeatedly.
Code Examples
// Array misused as a queue — O(n) shift on large arrays:
$queue = [];
array_push($queue, $item);
$next = array_shift($queue); // O(n) — reindexes entire array
// SplQueue — O(1) enqueue and dequeue:
$queue = new SplQueue();
$queue->enqueue($item);
$next = $queue->dequeue();
// SplStack — LIFO
$stack = new SplStack();
$stack->push('first'); $stack->push('second');
echo $stack->pop(); // 'second'
// SplQueue — FIFO
$queue = new SplQueue();
$queue->enqueue('job1'); $queue->enqueue('job2');
echo $queue->dequeue(); // 'job1'
// SplMinHeap — always extracts the minimum
$heap = new SplMinHeap();
$heap->insert(5); $heap->insert(1); $heap->insert(3);
echo $heap->extract(); // 1
// SplFixedArray — faster, less memory than array for integer-indexed data
$arr = new SplFixedArray(100);
$arr[0] = 'hello';