doudaotui4297 2015-03-05 09:20
浏览 52
已采纳

静态函数中包含文件的全局变量

I have index.php with included file strings.php for translating phrases. In strings.php is included file based on user language: $lang.php. This $lang.php file is automatically generated from $lang.ini and contains single array:

  <?php $translation = array (
      'USER_PROFILE' => 'Uživatelský profil',
      'MESSAGES' => 'Zprávy',
      'NOTIFICATIONS' => 'Oznámení',
      'SETTINGS' => 'Nastavení',
      ...

There is a class Strings with static function and globally assigned array $translation from $lang.php in strings.php:

include_once('cache/'.$lang.'.php');

class Strings {
    static public function translate($string) {
        global $translation;
        ...
    }
}

But $translation in translate() function returns null in this case. If I include $lang.php in index.php it suddenly works, but if I call Strings::translate() function in other file, $translation again returns null. I don't understand this behavior. (Sorry for english)

  • 写回答

1条回答 默认 最新

  • douqiang4501 2015-03-05 11:13
    关注

    Because the way the file $lang.php is included, the variable $translation lands as a local variable in a function and not as a global variable as method Strings::translate() expects it.

    After we discussed in chat and I understood the problem, I can suggest two solutions:

    Solution 1 (the quick workaround)

    Change file $lang.php:

    <?php global $translation = array (
        'USER_PROFILE' => 'Uživatelský profil',
        ...
    

    This way the variable $translation is created in the global context and everything works as expected.

    Solution 2 (a better design)

    Limit the scope of the variable $translation.

    File $lang.php:

    <?php return array (
        'USER_PROFILE' => 'Uživatelský profil',
        ...
    

    This way no variable is create (neither local nor global) and this removes the source of confusion.

    File strings.php:

    class Strings {
    
        static $translation;
    
        static private function initialize() {
             global $lang;
             static::$translation = include_once('cache/'.$lang.'.php');
        }
    
        static public function translate($string) {
            if (! isset(static::$translation)) {
                static::initialize();
            }
    
            // use static::$translation instead of global $translation
        }
    }
    

    Solution 3 (the correct design)

    An even better design (the correct one, in fact) is to make method initialize() public, change $lang as its parameter and call it from your code as soon as you determined the value of $lang.

    File strings.php:

    class Strings {
    
        static $translation;
    
        // Call this method once, as soon as you determined what
        // language you need to use
        static public function initialize($lang) {
             static::$translation = include_once('cache/'.$lang.'.php');
        }
    
        static public function translate($string) {
            // use static::$translation instead of global $translation
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?