5

I have never used anonymous functions in PHP before, but I found a piece of code that uses one in order to sort objects

usort($numTurnsPerUser,build_sorter('turns'));

function build_sorter($key) {
    return function ($a, $b) use ($key) {
        return strnatcmp($a[$key], $b[$key]);
    };
}

This code will sort an object by a key (I pass in "turns"). For example, an object that looks like this: (written in JSON, just for readability)

$numTurnsPerUser = {
    "31":{
        "turns":15,
        "order":0
    }, "36":{
        "turns":12, 
        "order":1
    }, "37":{
        "turns":14, 
        "order":2
    }
}

will be sorted into an object that looks like this:

$numTurnsPerUser = {
    "36":{
        "turns":12,
        "order":1
    }, "37":{
        "turns":14,
        "order":2
    }, "31":{
        "turns":15, 
        "order":0
    }
}

This worked great on my local server, which is running PHP 5.3.0, but it fails to work on my online server, which is running "php5" - I'm unable to find any information other than that. I am getting an error

Parse error: syntax error, unexpected T_FUNCTION

I read that PHP < 5.3 cannot use anonymous functions and must use create_function, but the "use" part of the anonymous function has me stumped. Could someone please explain to me what that "use" part of the function is, or better yet, how I can "translate" this to the create_function parameters needed?

5
  • 1
    Let's pretend create_function never existed... however, as noted (or discovered?), this is a PHP 5.3+ only feature. The Syntax Error is expected in PHP 5.2.999 and prior. I hope the title edits give this more direction. Happy coding :) Commented May 31, 2012 at 1:42
  • @pst if there's a better way to define this function other than create_function have at it - I just can't use an anonymous function Commented May 31, 2012 at 1:45
  • stackoverflow.com/questions/10590877/… It doesn't show "closures", but... seeing how the input is a string. (I can't imagine that this would be good in a larger scale, so perhaps create a class and use a method as the comparator "call-able"?) Commented May 31, 2012 at 1:48
  • And for how to use a(n object-bound) method as a callback: stackoverflow.com/questions/2838197/… Commented May 31, 2012 at 1:53
  • "which is running "php5" - I'm unable to find any information other than that." you can find out the version by running php --version on the command line. Or in the code of your PHP script, putting phpinfo(); will print out tons of info about your PHP installation Commented May 31, 2012 at 18:24

2 Answers 2

3

You could do like this:

Class Sorter {
  private $key;

  public function __construct($key) {
    $this->key = $key;
  }

  public function sort($a, $b) {
    return strnatcmp($a[$this->key], $b[$this->key]);
  }
}

usort($numTurnsPerUser, array(new Sorter('key_b'), 'sort'));
Sign up to request clarification or add additional context in comments.

Comments

0

A literal translation (since you specifically asked for create_function):

function build_sorter($key) {
    return create_function('$a, $b', '$key = '.var_export($key, true).';
        return strnatcmp($a[$key], $b[$key]);
    ');
}

Be careful if you run this a lot, because every time you call create_function it adds a global function that is never destroyed. If you do this too much it will run out of memory.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.