Search code examples
phpdrupaldrupal-7anonymous-function

PHP version 5.6.17 unexpected T function/anonymous function error


I have a local Drupal site running with MAMP on Win 7 with php 5.6.13. My live site has php 5.6.17. I have no problems on the local site, but on the live site I get unexpected T function error with the following code which I downloaded from drupal.org.

function redhen_activity_message_types() {
  $message_types = array_keys(message_type_load());
  $filtered = array_filter($message_types, function($var) {
    return strpos($var, 'redhen_') !== FALSE;
  });
  return $filtered;
}

The line beginning with $filtered is the one that is pointed to by the error message.

I know little about php, but have been searching the web and found something called anonymous functions not working on older versions of php. Is the slightly different versions of php causing this problem? How do I fix it?

Thanks!


Solution

  • The anonymous function is the function that's defined starting in that line of code:

    function($var) {
      return strpos($var, 'redhen_') !== FALSE;
    }
    

    I don't see anything obviously wrong with that function, and Redhen CRM gets enough use that others are likely to have run into problems already if it were clearly incorrect. And while it's always good to test under the same version as your production environment it's unlikely that there was bug introduced to PHP between 5.6.13 and 5.6.17 that's causing this problem.

    To find the root cause try a few things:

    1. Check to make sure the version of PHP you think is running in production is actually running. I've seen several environments recently where people had a different version running under php-cli than on the web server, meaning that running php --version on the command line gave the wrong answer. Use a phpinfo() call to verify the version.
    2. Check the Redhen issue queue for other people with the same problem.
    3. Update your test machine to match production. Once you're sure production is the version you think it is, set your dev environment to match and see if the problem comes back. If it does, try to move forward to a more current minor version and see if the error goes away again.