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  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
PHP

Collections and Sorting
By: David Fells
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 21
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek