If you need to use usort with a key in the calling method, I wrote this as a utility:
<?php
function usort_comparison($obj, $method, $key) {
$usorter = &new Usort($obj, $method, $key);
return array($usorter, "sort");
}
class Usort {
function __construct($obj, $method, $key) {
$this->obj = $obj;
$this->method = $method;
$this->key = $key;
}
function sort($a, $b) {
return call_user_func_array(array($this->obj, $this->method), array($a, $b, $this->key));
}
}
?>
<?php
require_once("util/usort.php");
class Foo {
$items = array(FooBar(13), FooBar(2));
public function sorter() {
usort($this-items, usort_comparison("Foo", "_cmp", "item"));
}
public static function _cmp($a, $b, $key) {
return strcasecmp($a->$key, $b->$key);
}
}
class FooBar {
public $item;
function __construct($val) {
$this->item = $val;
}
}
?>
~ simple example... but in the way I need to use it was the key was used in a switch statement to choose the different member of the object to compare against dynamically (as in, sort by x or y or z)