Referencing methods in PHP usort
The usort function in PHP allows you to sort data based on the results of a function that you define. But if you are using OO PHP and you want to avoid functions, it turns out you can use methods of objects too with a slight variation to the usort call. The usual call to usort looks something like this:
If you want to use a method in an object to act as the sorting function, you can do so by making the call like this:
You can also use this type of calling if the sorting method exists in the object that you are calling it from within. To do this, use this format:
Happy sorting!
usort($arrayToBeSorted, 'theNameOfAFunction');
If you want to use a method in an object to act as the sorting function, you can do so by making the call like this:
usort($arrayToBeSorted, array('theNameOfAnObject', 'theNameOfAMethod'));
You can also use this type of calling if the sorting method exists in the object that you are calling it from within. To do this, use this format:
usort($arrayToBeSorted, array($this, 'theNameOfAMethod'));
Happy sorting!
| Rating: | no ratings, 0 total Votes |
| Categories: | php coding programming |
| Added: | on Jun 13, 2008 at 11:18 am |
| Added By: | an anonymous user |

