
ArrayObject, SplFileInfo, SplSubject/SplObserverRecursiveIteratorIterator, FilterIterator, LimitIteratorArrayAccess, Countable, Iterator, IteratorAggregatespl_autoload_register(), iterator_to_array(), spl_object_hash()"A container is a class, a data structure, or an abstract data type whose instances are collections of other objects. They are used to store objects in an organized way following specific access rules."
We already have arrays and strings!


Versus traditional arrays, there's potential for:




It's best to use this when:
mysql_num_rows() function)1 2 3 4 5 6 7 8 9 10 11 12 | <?php$a = array();for ($i = 0; $i < $argv[1]; $i++) { $a[$i] = $i; $i = $a[$i];}$a = new SplFixedArray($argv[1]);for ($i = 0; $i < $argv[1]; $i++) { $a[$i] = $i; $i = $a[$i];} |



It's best to use this when:
1 2 3 4 5 6 7 8 9 10 11 12 | <?php$a = array();for ($i = 0; $i < $argv[1]; $i++) { $a[] = $i; $i = $a[$i];}$a = new SplDoublyLinkedList($argv[1]);for ($i = 0; $i < $argv[1]; $i++) { $a[] = $i; $i = $a[$i];} |



[] for both array and SplStackarray_pop() vs Splstack::pop()It's best to use this when:
1 2 3 4 5 6 7 8 | <?php$a = array();for($i = 0; $i < $argv[1]; $i++) { $a[] = $i;}for($i = 0; $i < $argv[1]; $i++) { array_pop($a);} |
1 2 3 4 5 6 7 8 | <?php$a = new SplStack;for($i = 0; $i < $argv[1]; $i++) { $a[] = $i;}for($i = 0; $i < $argv[1]; $i++) { $a->pop();} |



[] for both array and SplQueuearray_shift() vs SplQueue::dequeue()It's best to use this when:
1 2 3 4 5 6 7 8 | <?php$a = array();for($i = 0; $i < $argv[1]; $i++) { $a[] = $i;}for($i = 0; $i < $argv[1]; $i++) { array_shift($a);} |
1 2 3 4 5 6 7 8 | <?php$a = new SplQueue;for($i = 0; $i < $argv[1]; $i++) { $a[] = $i;}for($i = 0; $i < $argv[1]; $i++) { $a->dequeue();} |



[] + sort() vs SplHeap::insert()array_shift() vs SplHeap::extract()SplHeap::compare() can be overridden in subclassesSplMinHeap and SplMaxHeap
It's best to use this when:
1 2 3 4 5 6 7 8 9 | <?php$a = array();for($i = 0; $i < $argv[1]; $i++) { $a[] = rand(1, $argv[1]); sort($a);}for($i = 0; $i < $argv[1]; $i++) { array_shift($a);} |
1 2 3 4 5 6 7 8 | <?php$a = new SplMinHeap;for($i = 0; $i < $argv[1]; $i++) { $a->insert(rand(1, $argv[1]));}for($i = 0; $i < $argv[1]; $i++) { $a->extract();} |



SplPriorityQueue::compare() can be overridden in subclassesIt's best to use this when:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?phpfunction priority_sort($a, $b) { return $a[1] - $b[1];}$threshold = $argv[1] * 0.1;$a = array();$i = 0;do { if ($i <= $argv[1]) { $a[] = array($i, rand(1, 10)); usort($a, 'priority_sort'); } if ($i > $threshold) { array_shift($a); } $i++;} while (count($a)); |
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php$threshold = $argv[1] * 0.1;$a = new SplPriorityQueue;$i = 0;do { if ($i < $argv[1]) { $a->insert($i, rand(1,10)); } if ($i > $threshold) { $a->extract(); } $i++;} while (count($a)); |




spl_object_hash() function must be used for arrays to have
this capabilitySplObjectStorage::removeAllExcept() was not added until PHP 5.3.6It's best to use this when:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?php$a = array();for ($i = 0; $i < $argv[1]; $i++) { $object = new stdClass; $a[spl_object_hash($object)] = $object;}$a = array();$b = array();for ($i = 0; $i < $argv[1]; $i++) { $a[] = rand(1, $argv[1]); $b[] = rand(1, $argv[1]);}$c = array_merge($a, $b);$c = array_diff($a, $b); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?php$a = new SplObjectStorage;for ($i = 0; $i < $argv[1]; $i++) { $object = new stdClass; $a->attach($object, $object);}$a = new SplObjectStorage;$b = new SplObjectStorage;for ($i = 0; $i < $argv[1]; $i++) { $a->attach((object) rand(1, $argv[1])); $b->attach((object) rand(1, $argv[1]));}$c = clone $a;$c->addAll($b);$c = clone $a;$c->removeAll($b); |

