Php 静态函数中包含的文件中的全局变量
我有index.php,其中包含用于翻译短语的文件strings.php。在strings.php中包含基于用户语言的文件:$lang.php。此$lang.php文件由$lang.ini自动生成,包含单个数组:Php 静态函数中包含的文件中的全局变量,php,global,Php,Global,我有index.php,其中包含用于翻译短语的文件strings.php。在strings.php中包含基于用户语言的文件:$lang.php。此$lang.php文件由$lang.ini自动生成,包含单个数组: <?php $translation = array ( 'USER_PROFILE' => 'Uživatelský profil', 'MESSAGES' => 'Zprávy', 'NOTIFICATIONS' =>
<?php $translation = array (
'USER_PROFILE' => 'Uživatelský profil',
'MESSAGES' => 'Zprávy',
'NOTIFICATIONS' => 'Oznámení',
'SETTINGS' => 'Nastavení',
...
但在本例中,translate()函数中的$translation返回null。若我在index.php中包含$lang.php,它会突然工作,但若我在另一个文件中调用Strings::translate()函数,$translate再次返回null。我不理解这种行为。(对不起,英语)因为文件
$lang.php
包含的方式,变量$translation
作为函数中的局部变量,而不是方法字符串::translate()
所期望的全局变量
在聊天中讨论并了解问题后,我可以提出两种解决方案:
解决方案1(快速解决方案)
更改文件$lang.php
:
<?php global $translation = array (
'USER_PROFILE' => 'Uživatelský profil',
...
<?php return array (
'USER_PROFILE' => 'Uživatelský profil',
...
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
}
}
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
}
}
这样就不会创建任何变量(既不是局部变量,也不是全局变量),从而消除了混淆的根源
文件strings.php
:
<?php global $translation = array (
'USER_PROFILE' => 'Uživatelský profil',
...
<?php return array (
'USER_PROFILE' => 'Uživatelský profil',
...
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
}
}
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
}
}
解决方案3(正确的设计)
更好的设计(事实上是正确的)是将方法initialize()
公开,将$lang
更改为其参数,并在确定$lang
的值后立即从代码中调用它
文件strings.php
:
<?php global $translation = array (
'USER_PROFILE' => 'Uživatelský profil',
...
<?php return array (
'USER_PROFILE' => 'Uživatelský profil',
...
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
}
}
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
}
}
这取决于是否以及在何处包含$lang.php
。在调用translate()
之前,请确保将其包含在全局上下文中(而不是函数中)。我将其包含在同一文件中的类字符串之前(外部)。这个类在必要时自动加载,所以我猜它也包含$lang.php文件。是否?然后确保文件$lang.php
确实包含在内。放置一个echo($lang)代码>在包含之前(检查变量$lang
在该点是否可见并包含您期望的内容)和打印($translation)代码>包含后(检查文件是否实际包含)。在index.php
中的任何其他内容之前打开错误报告(error\u reporting(E_ALL&~E_NOTICE);
),或者使用require
而不是include
,以确保代码找到文件(假设$lang
正确)。或者只需在语言文件中放入一个echo()
,然后检查消息是否出现在输出中。我一直在报告错误。当include$lang.php
之后var\u dump($translation)
时,它正确地显示了这个数组,但是如果我在别处调用Strings::translate()
,它就不包括在内。我认为它与自动加载Strings类有关。现在我在Strings类中创建了私有函数。第一个定义$lang
变量,第二个包含$lang.php
并返回$translation
数组。在translate()
函数中,我调用了第二个one函数,而不是定义global,现在它可以工作了包含在$lang.php
中的code>不起作用,它应该是$translation=array(…);返回$translation代码>。