I'm working in the latest version of PHP, within a class, and writing a sorting function which includes the following:
public static function sort_alphabetically($data, $sortBy=null)
{
...
if($sortBy)
{
function sortBy($a, $b) use ($sortBy)
{
if ($a->$sortBy == $b->$sortBy){return 0;}
if ($a->$sortBy > $b->$sortBy){return 1;}
else{return -1;}
};
usort($data, "sortBy");
}
return $data;
}
yet I'm consistently getting
Parse error: syntax error, unexpected 'use' (T_USE), expecting '{'...
It's difficult to google the word use and get useful results, but I have seen a mention that it can't be used within a class or namespace. However, the PHP documentation does exactly that - http://php.net/manual/en/functions.anonymous.php - so I am not sure whether the author of that bit of the Internet was correct.
Can anyone shed some light on this? Is there an alternative method I could use?