Home arrow PHP arrow Page 3 - Collections and Sorting

Building the Foundation - PHP

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.

  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



In order to effectively implement our solution for creating sortable object collections, we need to determine what common properties the collection classes will have as well as the requirements for the objects that will be contained in a collection. For now, let us define two interfaces - ICollection for the collection classes and ISortable for the objects that need to be sorted. You will see the meaning of the ISortable name shortly.

interface ICollection
     public function Get($i);
     public function Append($object);
     public function Sort();

For now we are only going to support the three methods defined in the above interface. Get() will return an element by index, in lieu of an indexer. Append() will add a new element to the collection. Sort() will, of course, sort the elements in the collection.

interface ISortable
     public function GetSortKey();

This interface will be implemented by the objects inside the collection that need to be sorted. The GetSortKey() method will return the "key value" of the object, similarly to the way ToString() works in .NET or Java.

Now we need to define a basic abstract base class for our collection classes.

abstract class Collection implements ICollection
     protected $data;  

     public function __construct()
          $this->data = new ArrayObject();

     public function Get($i)
          return $this->data[$i];

     public function Append($object)

     public function Sort()

Here we have a basic implementation of the Collection class. It provides a default Get() and Append() method as well as a default constructor. Notice that the default implementation for the protected member $data is an ArrayObject. I chose this simply because I would prefer to be consistent and work with an object than the primitive type since both expose indexers. The only difference between using the ArrayObject and using an actual array is that we cannot directly access the inner array, which won't be a problem.

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- 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: