PHP
  Home arrow PHP arrow Page 3 - Collections and Sorting
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Collections and Sorting
By: David Fells
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 19
    2006-03-28

    Table of Contents:
  • Collections and Sorting
  • Weighing the Options
  • Building the Foundation
  • Concrete Classes

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Collections and Sorting - Building the Foundation


    (Page 3 of 4 )

    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


       · Thanks for reading!
       · I can't even begin to tell you how grateful I am that you posted this. I was totally...
     

       

    PHP ARTICLES

    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT