You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP8.3+Smarty5.0下implode/explode弃用警告及跨环境兼容问询

Smarty中implode/explode跨版本兼容方案

问题背景

收到弃用警告:

Deprecated: Using implode is deprecated. Use join using the array first, separator second.

使用explode时也收到类似警告,提示改用split,且官方渠道找不到相关文档。结合不同环境的测试结果:

测试用例(以implode为例)

1. {$array|join:","}
2. {","|join:$array}
3. {$array|implode:","}
4. {","|implode:$array}

PHP7.4 + Smarty3.x环境

  1. 触发警告:Deprecated: join(): Passing glue string after array is deprecated. Swap the parameters
  2. 正常运行
  3. 触发警告:Deprecated: implode(): Passing glue string after array is deprecated. Swap the parameters
  4. 正常运行

PHP8.3 + Smarty5.x环境

  1. 正常运行
  2. 触发警告:Deprecated: Using join with the separator first is deprecated. Call join using the array first, separator second.
  3. 触发警告:Deprecated: Using implode is deprecated. Use join using the array first, separator second.
  4. 触发警告:Deprecated: Using implode is deprecated. Use join using the array first, separator second.

目前implode/explode均会触发弃用警告,现有扩展代码如下:

$smarty->addExtension(new WildcardExtension());
class WildcardExtension extends \Smarty\Extension\Base {

    public function getModifierCallback(string $modifierName) {
        if (is_callable($modifierName)) {
            return $modifierName;
        }
        return null;
    }

}

兼容方案建议(无需单独模板修改)

1. 注册自定义兼容修饰器

直接为implode/explode注册适配回调,根据Smarty版本自动调整参数逻辑:

// 适配implode
$smarty->registerPlugin('modifier', 'implode', function($input, $separator) {
    $isSmarty4Plus = version_compare(\Smarty\Smarty::SMARTY_VERSION, '4.0.0', '>=');
    return $isSmarty4Plus ? join($input, $separator) : implode($separator, $input);
});

// 适配explode
$smarty->registerPlugin('modifier', 'explode', function($input, $separator) {
    $isSmarty4Plus = version_compare(\Smarty\Smarty::SMARTY_VERSION, '4.0.0', '>=');
    // PHP7+已移除split,统一用explode适配高版本Smarty要求
    return explode($separator, $input);
});

2. 修改现有WildcardExtension适配参数

在原有扩展中增加参数适配逻辑,自动兼容不同版本:

$smarty->addExtension(new WildcardExtension());
class WildcardExtension extends \Smarty\Extension\Base {

    public function getModifierCallback(string $modifierName) {
        if (!is_callable($modifierName)) {
            return null;
        }

        // 处理implode/join参数兼容
        if (in_array($modifierName, ['implode', 'join'])) {
            return function($input, $separator) use ($modifierName) {
                $isSmarty4Plus = version_compare(\Smarty\Smarty::SMARTY_VERSION, '4.0.0', '>=');
                if ($modifierName === 'join') {
                    return $isSmarty4Plus ? join($input, $separator) : join($separator, $input);
                } else {
                    // implode高版本转用join逻辑
                    return $isSmarty4Plus ? join($input, $separator) : implode($separator, $input);
                }
            };
        }

        // 处理explode/split参数兼容
        if (in_array($modifierName, ['explode', 'split'])) {
            return function($input, $separator) {
                // PHP7+已移除split,统一用explode适配所有版本
                return explode($separator, $input);
            };
        }

        return $modifierName;
    }

}

3. 全局编译拦截(不推荐)

通过修改Smarty编译流程,拦截修饰器调用并自动转换参数顺序,但实现复杂度高,易引发其他兼容问题,不建议采用。


内容的提问来源于stack exchange,提问作者Ben Coffin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 05:26:18