Smarty3 升级到 PHP7 和注册插件的奇怪问题

Smarty3 upgrade to PHP7 and weird problems with register plugin

我有一个现有的、功能良好的 PHP 应用程序。我正在研究服务器升级,其中包括从 PHP5 迁移到 PHP7。据我从文档中得知,Smarty3 应该与 PHP7 兼容。但是,在测试时,发现升级后Smarty拒绝编译模板。

问题的根源似乎是这一行:

$this->smarty->registerPlugin('compiler', 'asset_url',  array(&$this, 'asset_url'));

这会导致 PHP 应用程序像这样崩溃:

Notice: Array to string conversion in /usr/share/php/smarty3/sysplugins/smarty_internal_templatecompilerbase.php on line 415

Notice: Undefined property: template::$Array in /usr/share/php/smarty3/sysplugins/smarty_internal_templatecompilerbase.php on line 415

Fatal error: Uncaught Error: Function name must be a string in /usr/share/php/smarty3/sysplugins/smarty_internal_templatecompilerbase.php:415
Stack trace:
#0 /usr/share/php/smarty3/sysplugins/smarty_internal_templateparser.php(3585): Smarty_Internal_TemplateCompilerBase->compileTag('asset_url', Array)
#1 /usr/share/php/smarty3/sysplugins/smarty_internal_templateparser.php(4413): Smarty_Internal_Templateparser->yy_r32()
#2 /usr/share/php/smarty3/sysplugins/smarty_internal_templateparser.php(4515): Smarty_Internal_Templateparser->yy_reduce(32)
#3 /usr/share/php/smarty3/sysplugins/smarty_internal_smartytemplatecompiler.php(118): Smarty_Internal_Templateparser->doParse(3, '}')
#4 /usr/share/php/smarty3/sysplugins/smarty_internal_templatecompilerbase.php(283): Smarty_Internal_SmartyTemplateCompiler->doCompile('<!DOCTYPE html>...')
#5 /usr/share/php/smarty3/sysplugins/smarty_internal_template.php(197): Smarty_Internal_TemplateCompilerBase->compileTemplate(Object(Smarty_Internal_Template))
#6 /usr/share/php/smarty3/sysplugins/sm in /usr/share/php/smarty3/sysplugins/smarty_internal_templatecompilerbase.php on line 415

可疑行 415 如下​​所示:

 $function = $this->smarty->registered_plugins[$plugin_type][$tag][0];
 if (!is_array($function)) {
     return $function($new_args, $this);
 } elseif (is_object($function[0])) {
     return $this->smarty->registered_plugins[$plugin_type][$tag][0][0]->$function[1]($new_args, $this); <- Crash here!
 } else {
     return call_user_func_array($function, array($new_args, $this));
 }

我认为这是 PHP5 和 PHP7 之间的一些根本区别,这让我很头疼,但我似乎无法弄清楚它是什么。有人可以给我一些解决方法的建议吗?

如果您使用的是旧版本的 Smarty,您可能需要更新。 3.1.28 中为 PHP 7 兼容性添加了一些修复程序,可能会对此有所帮助。

https://github.com/smarty-php/smarty/blob/master/change_log.txt

这个问题的解释(除了像 "you have an outdated version" 这样最常见的问题)是在 PHP 7 中它已经改变了代码解析的方式。在你的情况下:

$this->smarty->registered_plugins[$plugin_type][$tag][0];

parsed differently in PHP 5 and 7:

PHP 5: $this->smarty->{registered_plugins[$plugin_type][$tag][0]};

PHP 7: ($this->smarty->registered_plugins)[$plugin_type][$tag][0];

您可以尝试通过放置大括号和方括号来修复这些代码片段,以指示解析器您的确切意图,但我建议您升级 Smarty。