Jump to content

[SOLVED] Defining usort callback function when it is another method of a class


GingerRobot

Recommended Posts

Does anyone know if it is possible to set the callback function of a user defined sort as another method of a class?

 

That might be a poor explanation, so perhaps some code will help to clarify:

 

<?php
class myclass{
    function mysort($a,$b){
        return $a - $b;
    }
    function pointlessfunction(){
        $array = array(4,3,1,4,5,7,2);
        usort($array,'mysort');//this is the problem
    }
}
$myclass = new myclass();
$myclass->pointlessfunction();
?>

 

The above gives an 'invalid comparison function' error, which is fair enough - normally when referencing functions in the same class, you use $this. However, i've tried the above with various replacements like:

 

usort($array,'this->mysort');
//or
usort($array,$this->mysort);

 

Without success. I realise that i could move the mysort function outside of the class, but i was wondering if it is possible to keep it within?

 

I hope that's clear enough. Thanks in advance for any help.

 

Ben

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.