PHP
  Home arrow PHP arrow 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? 
PHP

Collections and Sorting
By: David Fells
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 20
    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
    ( Page 1 of 4 )

    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.

    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
     

       

    PHP ARTICLES

    - 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
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    Stay green...Green IT