Andrei Herford
Andrei Herford

Reputation: 18745

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. 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.

So, what might be wrong here?

Upvotes: 1

Views: 179

Answers (1)

cmorrissey
cmorrissey

Reputation: 8583

You are trying to call someClass::sorterFunc which is not defined as it is actually myClass::sorterFunc because you are extending someClass with myClass.

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

Upvotes: 1

Related Questions