PHP Standard Library Strategies Quiz
A 35-question quiz exploring PHP SPL iterators, data structures, autoloading hooks, and stream helpers with 11 concise code walk-throughs for everyday review scenarios.
Question 1
Why do reviewers encourage teams to lean on SPL iterators instead of rolling ad-hoc iterator classes for every collection?
Question 2
What does this SplDoublyLinkedList snippet echo?
<?php
$list = new SplDoublyLinkedList();
$list->push('header');
$list->push('body');
echo $list->pop();
?>Question 3
Why would SplFixedArray be chosen for a CPU-friendly cache of 10k metrics instead of a plain PHP array?
Question 4
What filenames does this DirectoryIterator loop print?
<?php
$dir = new DirectoryIterator(__DIR__ . '/templates');
foreach ($dir as $file) {
if ($file->isFile()) {
echo $file->getFilename() . PHP_EOL;
}
}
?>Question 5
Why would a job scheduler reach for SplPriorityQueue when orchestrating retries?
Question 6
What does this SplObjectStorage snippet echo?
<?php
$storage = new SplObjectStorage();
$a = new stdClass();
$b = new stdClass();
$storage[$a] = 'alpha';
$storage[$b] = 'beta';
$storage->detach($a);
echo $storage[$b];
?>Question 7
Why is RecursiveIteratorIterator commonly used with RecursiveDirectoryIterator in deployment tooling?
Question 8
Why would SplQueue be favored for a producer-consumer worker while SplStack is saved for undo histories?
Question 9
What syllables does this LimitIterator example echo?
<?php
$inner = new ArrayIterator(['alfa', 'bravo', 'charlie']);
$limit = new LimitIterator($inner, 1, 2);
foreach ($limit as $word) {
echo substr($word, 0, 1);
}
?>Question 10
Why do file-processing daemons prefer SplFileObject over fopen/fgets pairs when tailing logs?
Question 11
Why are SplMaxHeap or SplMinHeap better suited for streaming leaderboards than sorting arrays on every update?
Question 12
What does this RegexIterator snippet output?
<?php
$it = new ArrayIterator(['error.log', 'readme.md', 'deploy.log']);
$regex = new RegexIterator($it, '/\.log$/');
foreach ($regex as $file) {
echo $file . '|';
}
?>Question 13
Why do plugin systems occasionally implement SplObserver and SplSubject instead of bespoke event arrays?
Question 14
Why must SplStack be rewound before reusing it in multiple foreach loops?
Question 15
What does this SplPriorityQueue snippet echo?
<?php
$q = new SplPriorityQueue();
$q->insert('critical', 10);
$q->insert('minor', 1);
$q->setExtractFlags(SplPriorityQueue::EXTR_DATA);
echo $q->extract();
?>Question 16
Why does SplFixedArray::setSize help long-running daemons avoid memory creep?
Question 17
Why is SplObjectStorage handy for tracking entities in memoization layers?
Question 18
What does this SplQueue snippet echo?
<?php
$q = new SplQueue();
$q->enqueue('task1');
$q->enqueue('task2');
$q->dequeue();
echo $q->bottom();
?>Question 19
Why do array-like DTOs often extend ArrayObject rather than using raw arrays?
Question 20
Why might CallbackFilterIterator be used in analytics pipelines instead of array_filter subsets?
Question 21
Why is SplMinHeap a natural fit for top-N dashboards that need the smallest value quickly?
Question 22
What does this SplFileObject snippet echo when reading a CSV line?
<?php
$file = new SplFileObject(__DIR__ . '/data/users.csv');
$file->setFlags(SplFileObject::READ_CSV);
$file->seek(1);
$record = $file->current();
echo $record[0];
?>Question 23
Why should custom SplSubject implementations always detach observers when shutting down?
Question 24
Why might SplDoublyLinkedList be configured in FIFO mode when modeling queues that sometimes need random access?
Question 25
Why do autoloading standards lean on spl_autoload_register rather than __autoload?
Question 26
What class name does this spl_autoload_register example load?
<?php
spl_autoload_register(function ($class) {
require __DIR__ . '/src/' . $class . '.php';
});
new Services\Mailer();
?>Question 27
Why is RecursiveDirectoryIterator often paired with filesystems that include symlink-heavy vendor trees?
Question 28
Why might extension authors implement SplObserver when exposing plugin hooks?
Question 29
What does this ArrayObject snippet echo after appending?
<?php
$list = new ArrayObject(['alpha']);
$list->append('beta');
$it = $list->getIterator();
foreach ($it as $value) {
$last = $value;
}
echo $last;
?>Question 30
Why do data-merging jobs use MultipleIterator when combining price feeds from separate iterators?
Question 31
Why does SplPriorityQueue extract elements in heap order even if you iterate with foreach?
Question 32
Why might SplFixedArray be converted to a regular array before passing data to json_encode?
Question 33
What does this SplMinHeap snippet echo?
<?php
$heap = new SplMinHeap();
$heap->insert(5);
$heap->insert(2);
$heap->insert(9);
echo $heap->extract();
?>Question 34
Why is SplObjectStorage valuable for access-control layers that need to track permissions per object instance?
Question 35
Why do asset pipelines inspect SplFileInfo objects before copying binaries?
