drogon982007 2013-11-06 13:03
浏览 31
已采纳

在PHP中使用变量进行本地化是否有任何缺点? [关闭]

Are there any drawbacks using variables against arrays for localization of a project in PHP?

E.g:

en.php

$string1 = "Open";

de.php

$string1 = "Öffnen";
  • 写回答

1条回答 默认 最新

  • drgmszo076956 2013-11-06 13:25
    关注

    I see the drawback, that if you declare 1000 variables you have 1000 zval containers with 1000 values. If you would declare it as an array like:

    en.php :

    $translations = array (
        'yes' => 'yes',
        'no'  => 'no'
    );
    

    ... then just a single zval container is required (with a larger value portion of course). This will help to keep PHP's internal symbol tables smaller and save memory. If you ask me, use an array.

    A more elaborated alternative would be to use the gettext extension


    Just for completeness, another drawback would be that using variables for translations could lead to almost any imaginable variable name, and therefore is highly prone for namespace pollution. If you should use variables (for whatever reason, I don't see), then you have to "namespace" them to avoid this, like:

    $translation_yes = 'yes';
    $translation_no = 'no';
    ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算