PHP anonymous functions scope question
- by Dan
Hi, I'm trying to sort an array of objects by a common property, however I cannot get my $property parameter to register in the inner function (I can use it in the outer one OK).
The way I read the documentation, it sounded like the parameter would be available, have I misunderstood something?
Here is what I have:
public static function sortObjectsByProperty($objects, $property)
{
function compare_object($a, $b)
{
$a = $a->$property;
$b = $b->$property;
if ($a->$property == $b->$property)
{
return 0;
}
return ($a->$property > $b->$property) ? +1 : -1;
}
usort($objects, 'compare_object');
return $objects;
}
Any advice appreciated.
Thanks.