0

I have a custom WordPress plugin that works fine on my local dev machine, but triggers an error on the production server.

The function:

function _iniloader_get_dirs($dir) {
        $dirs = array_filter(scandir($dir), function ($item) use ($dir) {
            return (is_dir($dir.'/'.$item) && $item != "." && $item != "..");
        });
        // Use array_values to reset the array keys:
        return array_values($dirs);
}

The error:

Parse error: syntax error, unexpected T_FUNCTION in (in plugin) on line 30

Line 30 is the second line of the function.

My local MAMP server = PHP Version 5.3.6
Linux production server = PHP Version 5.3.5

Does anyone have an idea what the issue could be, and why it would show up in one environment but not the other?


UPDATE:

I just noticed that, if I put this function in a regular PHP file on the production box, it executes fine- so it's only triggering the error when it's part of the WordPress plugin, which makes even less sense to me...

Yarin
  • 173,523
  • 149
  • 402
  • 512
  • 1
    You are probably mistaken about the production server. Use `print PHP_VERSION;` in the invocation script to find out the version. – mario Jan 17 '12 at 15:29
  • It may also be useful if you could provide us with the difference between the phpinfo() function output on the two machines. – Stephen Rudolph Jan 17 '12 at 15:31
  • 1
    I could imagine wordpress is foreced to an older php version via htaccess or something. Please post the ouptput of `phpversion()` called from inside the Wordpress plugin. – DerVO Jan 17 '12 at 15:35
  • Took Mario's advice and sure enough, `print PHP_VERSION` within WordPress shows 5.2.11 (phpinfo() on the server showed 5.3, didn't realize WP would be running on something else...) – Yarin Jan 17 '12 at 15:37

1 Answers1

2

Likely it's your usage of Closures.

These became available in PHP > 5.3. Your Production server is either not running 5.3 or a minor version that might be buggy.

Refactor the use clause and I imagine it will run on both environments.

Jason McCreary
  • 71,546
  • 23
  • 135
  • 174
  • Looks like you're right. Can you provide guidance on how to convert this to <5.3? How exactly would you refactor the `use` clause without rewriting the whole function? – Yarin Jan 17 '12 at 15:38
  • Unfortunately no. You will have to use a [traditional callback](http://www.php.net/manual/en/language.pseudo-types.php#language.types.callback) which will require you to *rewrite the whole function*. – Jason McCreary Jan 17 '12 at 15:55