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.

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

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)
     {
          $this->data->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
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap

Dev Shed Tutorial Topics: