Home arrow PHP arrow Collections and Sorting

Collections and Sorting

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.

TABLE OF CONTENTS:
  1. Collections and Sorting
  2. Weighing the Options
  3. Building the Foundation
  4. Concrete Classes
By: David Fells
Rating: starstarstarstarstar / 21
March 28, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Collections and Sorting

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:

  1. There is no way to directly access the ArrayObject's internal array.
  2. 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.



 
 
>>> More PHP Articles          >>> More By David Fells
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: