【问题标题】:Using usort in php with a class private function在带有类私有函数的 php 中使用 usort
【发布时间】:2011-08-28 13:56:17
【问题描述】:

ok 用一个函数使用 usort 没那么复杂

这是我之前的线性代码中的内容

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

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

我只是简单地排序

usort($array,"merchantSort");

现在我们正在升级代码并删除所有全局函数并将它们放在适当的位置。现在所有代码都在一个类中,我不知道如何使用 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
   ...

   }
}

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

【问题讨论】:

    标签: php arrays sorting object


    【解决方案1】:

    使您的排序功能静态:

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

    并为第二个参数使用数组:

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

    【讨论】:

    • 这太棒了!我还想指出,排序函数不必隐含地声明为静态方法;因为它仍然可以在没有 :) 的情况下工作
    • @Jimbo - 这是有道理的,所以私有函数可以使用实例化和类变量。是的,这太棒了!另请参阅@deceze 答案,您可以在其中传递$this (neato)。
    • 如果你将函数设为静态(你应该这样做),你可以写usort($array, 'ClassName:merchantSort'),不是吗?
    • 伙计,这似乎是一种奇怪的方式。哦 PHP,我们多么爱你。
    • @MarcoW.,我认为 ClassName 和 MerchantSort 之间缺少第二个“:”。此外,如果该函数在同一个类本身中使用,我已经使用'self::merchantSort' 对其进行了测试,并且它正在工作。
    【解决方案2】:
    1. 打开手册页http://www.php.net/usort
    2. 看到$value_compare_func 的类型是callable
    3. 点击链接的关键词即可到达http://php.net/manual/en/language.types.callable.php
    4. 看到语法是array($this, 'merchantSort')

    【讨论】:

      【解决方案3】:

      您需要传递$this 例如:usort( $myArray, array( $this, 'mySort' ) );

      完整示例:

      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 ) );
      

      【讨论】:

        【解决方案4】:

        在本例中,我按数组中名为 AverageVote 的字段进行排序。

        您可以在调用中包含该方法,这意味着您不再有类范围问题,就像这样...

                usort($firstArray, function ($a, $b) {
                   if ($a['AverageVote'] == $b['AverageVote']) {
                       return 0;
                   }
        
                   return ($a['AverageVote'] < $b['AverageVote']) ? -1 : 1;
                });
        

        【讨论】:

        • 仅当您仅在这一类中使用此功能时才有意义。在许多情况下,许多地方都使用相同的比较。
        • 这非常适合我需要做的事情。谢谢!
        【解决方案5】:

        在 Laravel (5.6) 模型类中,我是这样称呼的,两个方法都是public static,在windows 64位上使用php 7.2。

        public static function usortCalledFrom() 
        
        public static function myFunction()
        

        我确实像这样调用了 usortCalledFrom()

        usort($array,"static::myFunction")
        

        这些都不行

        usort($array,"MyClass::myFunction")
        usort($array, array("MyClass","myFunction")
        

        【讨论】:

        • static:: 代替类名是我需要的,谢谢提及。
        猜你喜欢
        • 1970-01-01
        • 2016-04-22
        • 2017-11-04
        • 1970-01-01
        • 2011-03-22
        • 1970-01-01
        • 2012-02-14
        • 2015-04-02
        • 1970-01-01
        相关资源
        最近更新 更多