Using usort in a class

Using usort in a class

am 22.04.2010 20:12:05 von Ashley Sheridan

--=-ni8+XbsTRaXPKJAe8IXh
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

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



--=-ni8+XbsTRaXPKJAe8IXh--

Re: Using usort in a class

am 22.04.2010 20:25:55 von Ashley Sheridan

--=-W44acRO2vU50+0y5+L2J
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

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

> On Thu, Apr 22, 2010 at 2:12 PM, Ashley Sheridan
> 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



--=-W44acRO2vU50+0y5+L2J--

Re: Using usort in a class

am 22.04.2010 20:26:56 von Adam Richardson

--00148530af20a7ac6c0484d774ee
Content-Type: text/plain; charset=ISO-8859-1

On Thu, Apr 22, 2010 at 2:12 PM, Ashley Sheridan
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

--00148530af20a7ac6c0484d774ee--

Re: Using usort in a class

am 22.04.2010 20:39:26 von Adam Richardson

--00c09f923843578f700484d7a1c2
Content-Type: text/plain; charset=ISO-8859-1

On Thu, Apr 22, 2010 at 2:25 PM, Ashley Sheridan
wrote:

> On Thu, 2010-04-22 at 14:26 -0400, Adam Richardson wrote:
>
> On Thu, Apr 22, 2010 at 2:12 PM, Ashley Sheridan
> 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
>
>
>
No problem ;)

Adam

--
Nephtali: PHP web framework that functions beautifully
http://nephtaliproject.com

--00c09f923843578f700484d7a1c2--