在带有类私有函数的 php 中使用 usort
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6053994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using usort in php with a class private function
提问by Ibu
ok using usort with a function is not so complicated
好的,在函数中使用 usort 并没有那么复杂
This is what i had before in my linear code
这是我之前在线性代码中的内容
function merchantSort($a,$b){
return ....// stuff;
}
$array = array('..','..','..');
to sort i simply do
排序我只是做
usort($array,"merchantSort");
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
现在我们正在升级代码并删除所有全局函数并将它们放在适当的位置。现在所有代码都在一个类中,我不知道如何使用 usort 函数对数组进行排序,参数是对象方法而不是简单函数
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
...
}
}
The question is how do i call an object method inside the usort() function
问题是如何在 usort() 函数中调用对象方法
回答by Demian Brecht
Make your sort function static:
使您的排序功能静态:
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'));
回答by deceze
- open the manual page http://www.php.net/usort
- see that the type for
$value_compare_func
iscallable
- click on the linked keyword to reach http://php.net/manual/en/language.types.callable.php
- see that the syntax is
array($this, 'merchantSort')
- 打开手册页http://www.php.net/usort
- 看到的类型
$value_compare_func
是callable
- 单击链接的关键字以访问http://php.net/manual/en/language.types.callable.php
- 看到语法是
array($this, 'merchantSort')
回答by Justin
You need to pass $this
e.g.: usort( $myArray, array( $this, 'mySort' ) );
你需要通过$this
例如:usort( $myArray, array( $this, 'mySort' ) );
Full example:
完整示例:
class SimpleClass
{
function getArray( $a ) {
usort( $a, array( $this, 'nameSort' ) ); // pass $this for scope
return $a;
}
private function nameSort( $a, $b )
{
return strcmp( $a, $b );
}
}
$a = ['c','a','b'];
$sc = new SimpleClass();
print_r( $sc->getArray( $a ) );
回答by James K
In this example I am sorting by a field inside the array called AverageVote.
在此示例中,我按数组中名为 AverageVote 的字段进行排序。
You could include the method inside the call, which means you no longer have the class scope problem, like this...
您可以在调用中包含该方法,这意味着您不再有类范围问题,就像这样......
usort($firstArray, function ($a, $b) {
if ($a['AverageVote'] == $b['AverageVote']) {
return 0;
}
return ($a['AverageVote'] < $b['AverageVote']) ? -1 : 1;
});
回答by hrnsky
In Laravel (5.6) model class, I called it like this, both methods are public static, using php 7.2 on windows 64 bit.
在 Laravel (5.6) 模型类中,我这样称呼它,两种方法都是 public static,在 windows 64 位上使用 php 7.2。
public static function usortCalledFrom()
public static function myFunction()
I did call in usortCalledFrom() like this
我确实像这样调用了 usortCalledFrom()
usort($array,"static::myFunction")
None of these were work
这些都不是工作
usort($array,"MyClass::myFunction")
usort($array, array("MyClass","myFunction")