Re: Using usort in a class

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, 2010-04-22 at 14:26 -0400, Adam Richardson wrote:

> On Thu, Apr 22, 2010 at 2:12 PM, Ashley Sheridan
> <ash@xxxxxxxxxxxxxxxxxxxx> wrote:
> 
>         I've not had to do this before, and now that I am, I've hit a
>         bit of a
>         wall:
>         
>         Basically, I've an array that might look like this (the number
>         of
>         elements may vary, but the letter is always unique and remains
>         a single
>         character):
>         
>         Array(
>            0 => '2h'
>            1 => '1d'
>            2 => '2w'
>         )
>         
>         And I need to sort them according to their letter. If I wasn't
>         using
>         classes, I could just use usort($array, 'custom_sort') but
>         when I do
>         that within my class, I'm told it can't find the function.
>         I've tried
>         usort($array, $this::custom_sort) and usort($array,
>         $Gantt_Task::custom_sort), both of which throw up the rather
>         frightening
>         unexpected double colon error (if you've ever seen one you'll
>         know what
>         I'm on about!)
>         
>         Does anyone know how I can do a usort within a class without
>         resorting
>         to making my sorting function a global function that isn't
>         part of the
>         class? I'd rather keep this tidy and everything in the class
>         that needs
>         to be in it, so a random function sitting outside is something
>         I want to
>         avoid if possible.
>         
>         Alternatively, if anyone knows a better way than using usort
>         I'm all
>         ears!
>         
>         Thanks,
>         Ash
>         http://www.ashleysheridan.co.uk
>         
>         
> 
> 
> Try using an array to pass in the function:
> 
> 
> 
> usort($array, array('ClassName', 'staticMethodName'));
> 
> 
> Adam
> 
> 
> -- 
> Nephtali:  PHP web framework that functions beautifully
> http://nephtaliproject.com
> 

Thanks! I literally just found that answer as your reply came through.
It's a bit of a facepalm moment here right now, as I'm ashamed to say
that the answer was on the manual pages all along, which I'd failed to
read properly :-/

Thanks,
Ash
http://www.ashleysheridan.co.uk



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux