[nycphp-talk] Callback function in a class
corey szopinski
corey at domanistudios.com
Wed Jun 22 17:58:43 EDT 2005
I have a need to use a callback function from uasort within a class, and I¹m
having a hard time figuring out how to pass the name of the custom sort
method in as the second parameter (it¹s a string... not a reference).
For example, here the basic code structure:
class StoreLocator{
function searchByZip($zip){
[ some code that builds a store array ]
$stores[0]['name'] = "store1";
$stores[0]['distance'] = "3.4";
$stores[0]['name'] = "store2";
$stores[0]['distance'] = "5.6";
uasort($stores, 'sortDistance');
return $stores;
}
function sortDistance($a,$b){
if($a['distance'] == $b['distance']) return 0;
return ($a['distance'] > $b['distance']) ? 1 : -1;
}
}
So, in the code
uasort($stores, 'sortDistance');
doesn't work, I've also tried:
$sortDistance &= $this->sortDistance();
uasort($stores, 'sortDistance');
This technique is able to find the method sortDistance, but isn't able to
pass in the ($a, $b) arrays to sort.
The last thing I've tried is
$sortDistance &= $this->sortDistance($a, $b);
uasort($stores, 'sortDistance');
Which also doesn't work. Any ideas?
- Corey
More information about the talk
mailing list