GingerRobot Posted February 7, 2008 Share Posted February 7, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/89967-solved-defining-usort-callback-function-when-it-is-another-method-of-a-class/ Share on other sites More sharing options...
rhodesa Posted February 7, 2008 Share Posted February 7, 2008 try usort($array,array($this,'mysort')); and read more about it here: http://us2.php.net/callback Quote Link to comment https://forums.phpfreaks.com/topic/89967-solved-defining-usort-callback-function-when-it-is-another-method-of-a-class/#findComment-461219 Share on other sites More sharing options...
GingerRobot Posted February 7, 2008 Author Share Posted February 7, 2008 Perfect. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/89967-solved-defining-usort-callback-function-when-it-is-another-method-of-a-class/#findComment-461226 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.