PHP5+ array-like classes: great new flavors and functionality!

This is a brief summary of this excellent article, just to keep myself from forgetting these exist.

Fixed array  (SplFixedArray) Рwhen the number of elements to be stored is known in advance and the elements only need to be accessed by sequential position (as in a loop).

Doubly-linked List (SplDoublyLinkedList) – makes sense when the number of elements to be stored is not known in advance and the elements only need to be accessed by sequential position. (Bonus: each element in the list includes a reference to the elements on either side of it within the list)

Stack (SplStack) – makes sense when the number of elements to be stored is not known in advance and the only element that must be accessible is the last one stored.

Queue (SplQueue) – makes sense when the number of elements to be stored is not known in advance and the only element that must be accessible is the remaining element that was stored earliest.

Heap (SplHeap) – makes sense when the number of elements to be stored is not known in advance and elements must be accessed in an order based on how they compare to each other. Issues:

Priority Queue (SplPriorityQueue) – when the number of elements to be stored is not known in advance and elements must be accessed in an order based on how a value associated with each element (versus the element value itself) compares to the same associated values of other elements.

Objects, Sets, and Composite Hash Maps (SplObjectStorage) – makes sense when data must be stored using composite keys or the ability to access data using set operations is more important than accessing data in a specific order.

Leave a Reply