由于PHP5.2中的匿名函数问题,在服务器上获取错误

由于PHP5.2中的匿名函数问题,在服务器上获取错误,php,google-analytics,google-analytics-api,Php,Google Analytics,Google Analytics Api,我正在使用“GoogleAPI php客户端”库,它在本地系统上运行良好,但在服务器上出现以下错误,因为它的版本是5.2 syntax error, unexpected T_FUNCTION, expecting ')' 所以我这里有两个问题,我们是否可以通过对代码进行一些更改来修复这个错误,使它与这个函数一起工作?下面是autoload.php的代码 spl_autoload_register( function ($className) { $classPath = explode(

我正在使用“GoogleAPI php客户端”库,它在本地系统上运行良好,但在服务器上出现以下错误,因为它的版本是5.2

syntax error, unexpected T_FUNCTION, expecting ')'
所以我这里有两个问题,我们是否可以通过对代码进行一些更改来修复这个错误,使它与这个函数一起工作?下面是autoload.php的代码

spl_autoload_register(
function ($className) {
  $classPath = explode('_', $className);
  if ($classPath[0] != 'Google') {
    return;
  }
  // Drop 'Google', and maximum class file path depth in this project is 3.
  $classPath = array_slice($classPath, 1, 2);

  $filePath = dirname(__FILE__) . '/' . implode('/', $classPath) . '.php';
  if (file_exists($filePath)) {
    require_once($filePath);
  }
}
);

但我不知道如何改变上述内容来解决这个问题,也不知道是否有任何库可以在php 5.2版上运行?如果我使用它,它可能会在某些其他功能上出现错误。谢谢

您的php版本似乎不知道匿名函数或闭包。尝试使用命名的一个:

function autoloadGoogleApi($className) {
  $classPath = explode('_', $className);
  if ($classPath[0] != 'Google') {
    return;
  }
  // Drop 'Google', and maximum class file path depth in this project is 3.
  $classPath = array_slice($classPath, 1, 2);

  $filePath = dirname(__FILE__) . '/' . implode('/', $classPath) . '.php';
  if (file_exists($filePath)) {
    require_once($filePath);
  }
}

spl_autoload_register('autoloadGoogleApi');
不过,我还想指出,您指定的php版本非常旧,所以我建议您考虑升级


UPD:

PHP5.2已经死了好几年了。您升级的时间已经过去了。@PaulCrovella是的,您是对的,但我正在处理的项目是在这个版本中构建的,升级版本非常危险,因为它可能会干扰一切,因此这将是我们升级的最后一个选项。有没有其他方法可以帮上忙?它解决了问题,但正如预期的那样,开始出现其他错误。升级版本可能是剩下的唯一解决方案。5.2版有可用的库吗?我搜索了,但还没有找到任何东西。@mÄrMÄlîk最好是修补到新的php版本。这可能是一个痛苦而缓慢的过程,但大多数库已经这样做了。或者,如果您不开始修补,您将进一步陷入版本兼容性问题。考虑