PHP has only a limited ability to support collections in the way that other programming languages such as C# and Java do, as far as the manner of access. This article navigates one possible solution.
A collection is an object whose primary function is to store a number of like objects. An object called CarCollection may contain any number of Car objects. Collections can traditionally be accessed in the same manner as arrays, which means CarCollection[n] represents a particular Car object. This is true in C#, Java, and more - but not PHP, unfortunately. Since PHP has only recently begun to develop a package of built in objects (the SPL, Standard PHP Library), the ability to support collections in the accepted behavioral sense is very limited.
There is no way in PHP to create a custom indexer, which means if you want to use an object that allows direct access to elements via the Object[n] notation, you must inherit from the ArrayObject class. This means you are not able to change the internal storage method, which, in the case of ArrayObject, is a native array. Since there is no data structure in PHP other than the array, though, this implementation is acceptable; there isn't any other alternative that would not eventually resolve to a native array.
"Wait," you say, "what about sorting?" Using the native PHP array, there are an abundance of ways to sort. Since the solution we have discussed so far uses an array for internal data storage, this should be OK - but it is not. We are faced with two problems:
There is no way to directly access the ArrayObject's internal array.
Assuming issue 1 is resolved, any given collection must know how to sort the object it contains - remember, native array sort functions only work with primitive types (strings and numbers).
Additionally there is the consideration of how we will sort. There are a number of available sorting algorithms, several of which are reasonably close in speed, several of which are archaic and nearly useless. We will examine some of the available choices later in this article and determine which is the best choice for our sorting needs.