简体   繁体   English

uksort不起作用-回调未执行或异常

[英]uksort does not work - Callback not executed or exception

I am trying to use uksort in PHP 5.5 to sort an array by its key. 我试图在PHP 5.5中使用uksort通过其键对数组进行排序。 This does not work. 这是行不通的。 Either an exception is thrown, that the specified class is invalid or nothing happens: 抛出异常,或者指定的类无效或什么也没有发生:

namespace some\name\space

class myClass extends someClass {
   public function someFunction {
       $theArray = array(
           "key1" => "value1",
           "key2" => "value2",
           ...
           "keyN" => "valueN"
       );

       // Exception "Fatal Error: Class name must be a valid object or a string"
       uksort($theArray, array($this, "sorterFunc"));
       uksort($theArray, array("someClass", "sorterFunc"));
       uksort($theArray, array("some\name\space\someClass", "sorterFunc"));
       uksort($theArray, "self::sorterFunc");
       uksort($theArray, "some\name\space\someClass::sorterFunc");

       // No effect. No logging from sorterFunc and 
       // key/value order stays the same.
       uksort($theArray, "sorterFunc");
       uksort($theArray, "someClass::sorterFunc");
   }

   public static function sorterFunc($a, $b) {
       myLogger->info("sorterFunc called");
       return $this->CompareASomehowToB($a, $b);
   }
}

So none of the calls I found in the docu or in any example work. 因此,我在文档或任何示例中找不到的呼叫都起作用。 When I skip uksort and use call_user_func the result is exactly the same. 当我跳过uksort并使用call_user_func ,结果完全相同。

So, what might be wrong here? 那么,这里可能出什么问题了?

您正在尝试调用someClass::sorterFunc ,因为它实际上是myClass::sorterFunc因为您正在使用myClass扩展someClass

uksort($theArray, "myClass::sorterFunc");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM