Training, Open Source computer languages

PerlPythonMySQLTclRubyC & C++LuaJavaTomcatPHPhttpdLinux

Search our site for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Sorting objects in PHP

The OO model in PHP is very powerful indeed - and much more code is now written using the facilities it offers than was the case a year or two ago. On yesterday's Object Oriented Programming in PHP course, I wrote a new example that nicely brought together many of the facilities on offer - you can see the full sample code here.

One very common requirement is to sort an array of objects in PHP, and if you just use the regular sort function, you'll end up sorting (I think) based on the memory address at which they are held. That is never what you want! Instead - you should use usort

Here's my example of a call to usort:

usort($wellhousemanor,array("transact","mvf"));

And that sorts an array of objects in the $wellhousemanor array, using the static member function called mvf in the class transact to compare pairs of records. All you (as the application programmer) need do is make the call in this (rather curious, it must be said) way. If you're the programmer writing the class, you simply provide a method of the given name that takes two parameters and return negative if the first object passed should come earlier, positive if the second object passed should come earlier, and zero to indicate that the objects are equally ranked for sorting.

Here's my example comparator:

static function mvf($first,$second) {
if ($first->getvalue() > $second->getvalue()) return 1;
if ($first->getvalue() < $second->getvalue()) return -1;
return 0;
}


Q: I'm a PHP programmer - should I use objects these days?

A: In most cases YES - exceptions are minimum maintainance jobs on older code, and tiny applications where you'll never have more than a few lines of PHP. And even if you're not writing your own objects, chances are that you'll use objects that other people have written and perhaps contributed via something like PEAR.
(written 2008-10-04 05:47:07)

 
Associated topics are indexed under
H108 - Objects in PHP

Back to
Calling base class constructors
Previous and next
or
Horse's mouth home
Forward to
Autumn
Some other Articles
Which is your best hotel room?
FSB - an update.
Claverton Pump
Autumn
Sorting objects in PHP
Calling base class constructors
Icelandic Badge
Marc Schneider is still having email trouble
Holt on holt
Hotel Guest Surveys
1892 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

© WELL HOUSE CONSULTANTS LTD., 2008: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 0800 043 8225 or 01225 708225 • FAX: 0845 8382 405 or 01225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho