I'm having a problem with sorting items in a PHP class that extends ArrayObject.
I am creating my classes, and the only way I've figured out to add the cmp() function is to put it in the same file, but outside of the class. I can't seem to put it anyplace else because of the way uasort requires the function name as a string.
So I'm doing this:
class Test extends ArrayObject{
public function __construct(){
$this[] = array( 'test' => 'b' );
$this[] = array( 'test' => 'a' );
$this[] = array( 'test' => 'd' );
$this[] = array( 'test' => 'c' );
}
public function sort(){
$this->uasort('cmp');
}
}
function cmp($a, $b) {
if ($a['test'] == $b['test']) {
return 0;
} else {
return $a['test'] < $b['test'] ? -1 : 1;
}
}
Which is fine if I'm only using one class like this, but if I use two (either by autoloading or require) then it breaks on trying to invoke cmp() twice.
I guess my point is it just seems like a bad way to do this. Is there any other way I can keep the cmp() function inside the class itself?