Reputation: 490657
I recently really wanted to us anonymous functions in PHP. Unfortunately my host is still on 5.2
. I automatically thought this would work:
uasort($array, function($a, $b) {
return $a > $b;
});
Is this how they work? Simply passed in as an argument instead of a callback? The docs don't say specifically that's how they do, but I have a working knowledge of JavaScript's anonymous functions, so I assumed they would.
Upvotes: 0
Views: 947
Reputation: 61597
Yes. You can use it in place of regular PHP callbacks.
Try this (in PHP 5.3):
function wait($callback)
{
sleep(10);
call_user_func($callback);
}
wait(function(){
echo "Hello!";
});
How call_user_func()
works is it will accept any of the following:
'functionName'
array('className', 'methodName')
array($objectInstance, 'methodName');
and now in PHP 5.3
function(){ // .. do something ..
}
My guess is that internal PHP functions user call_user_func()
for callbacks, and because it has support for anonymous functions, they will work just as well as other callbacks.
Upvotes: 1