> function sort_by_key($key) {

For the record, I do like this syntax, but it COULD be rewritten using
current practices (see below).

class SortByKey {
  public $key;
  public function __construct($key) {
    $this->key = $key;
  }
  public function do($a, $b) {
   if ($a[$this->key] < $b[$this->key])
     return -1;
   if ($a[$this->key] == $b[$this->key])
     return 0;
   return 1;
 }
}

$a = array(
array('id' => 0, 'first_name' => 'john', 'last_name' => 'connor'),
...
);
$Sorter = new SortByKey('last_name');
usort($a, array($Sorter, 'do'));


That said, I like the idea of a first class callable object.
For the example above, doing this:

class SortByKey implements Callable {
...
$Sorter();

Would actually execute $Sorter->do(); (obviously there's a problem when
$Sorter has a __toString (dynamic function call), but this seems to
nicely wrap all but the most basic case of callback, where an object
might be overkill.

Perhaps this could be implemented in SPL? (Just wondering if it's
possible at this point, I'm not campaigning for it..)

S

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to