Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected T_FUNCTION?

Tags:

php

I've question about solving in php version 5.2.7, where i get an error Parse error: syntax error, unexpected T_FUNCTION in /home/ ... /mainMenu.php on line 56. The code is

class MainMenu {
    ...
    private static function toRec($arr) {
        ...

        usort($newArr, function($a, $b) {//this was line 56
            return $a['nav_order'] - $b['nav_order'];
        });
        ...
    }
    ...

}

What is alternative for php 5.2?

Thank you

like image 935
user2274867 Avatar asked Dec 27 '22 05:12

user2274867


1 Answers

As you've discovered, the inline function syntax is only valid in PHP 5.3 an higher. It is not available in PHP 5.2.

The alternative is to specify the name of a function instead, as a string, and then declare the function separately with that name. This is documented fairly well in the usort() manual page, so I won't go into detail here.

You can also use create_function(). This may be the closest way to make your PHP 5.2 code look like 5.3 visually, but I would strongly recommend against this for a number of reasons.

Finally, I would very strongly recommend upgrading away from 5.2. I know there are cases where this is difficult, but that fact is that PHP 5.2 was declared end of life more than two years ago; it has not had any security updates in that time, and there are some big holes in it. If you're still stuck on 5.2 then you are falling further and further behind the curve; even 5.3 will be end-of-life in the near future, as 5.5 is due out fairly soon now.

like image 149
Spudley Avatar answered Jan 08 '23 04:01

Spudley