在PHP中使用usort带班私有函数 [英] Using usort in php with a class private function

查看:145
本文介绍了在PHP中使用usort带班私有函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OK使用usort带功能并不复杂。

ok using usort with a function is not so complicated

这是我在我的线性code收到了

This is what i had before in my linear code

function merchantSort($a,$b){
    return ....// stuff;
}

$array = array('..','..','..');

排序我根本就

usort($array,"merchantSort");

现在我们正在升级code和删除所有全局函数,并把他们在适当的位置。现在所有的code是一类,我无法弄清楚如何使用usort函数来这是一个对象的方法,而不是简单的功能

Now we are upgrading the code and removing all global functions and putting them in their appropriate place. Now all the code is in a class and i can't figure out how to use the usort function to sort the array with the parameter that is an object method instead of a simple function

class ClassName {
   ...

   private function merchantSort($a,$b) {
       return ...// the sort
   }

   public function doSomeWork() {
   ...
       $array = $this->someThingThatReturnAnArray();
       usort($array,'$this->merchantSort'); // ??? this is the part i can't figure out
   ...

   }
}

问题是我如何调用usort()函数中的对象的方法

The question is how do i call an object method inside the usort() function

推荐答案

请您排序功能的静态:

private static function merchantSort($a,$b) {
       return ...// the sort
}

和使用数组的第二个参数:

And use an array for the second parameter:

$array = $this->someThingThatReturnAnArray();
usort($array, array('ClassName','merchantSort'));

这篇关于在PHP中使用usort带班私有函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆