Php 数组和类
我有以下问题 我有一个文件en-en.php——我在其中以如下方式保存所有功能翻译:Php 数组和类,php,arrays,Php,Arrays,我有以下问题 我有一个文件en-en.php——我在其中以如下方式保存所有功能翻译: <?php $lang = array( 'Index' => 'Index' ); ?> 然而,这种方式不允许我显示实际的单词。有什么问题吗 标题中包含我的姓名: include '/lang/en-EN.php'; include '/theme/classes/User.class.php'; $user = new User(); $us
<?php
$lang = array(
'Index' => 'Index'
);
?>
然而,这种方式不允许我显示实际的单词。有什么问题吗
标题中包含我的姓名:
include '/lang/en-EN.php';
include '/theme/classes/User.class.php';
$user = new User();
$user->showIndex();
我需要以某种方式传递$lang数组,但我不知道如何 类和类的方法具有不同的变量范围,因此它们看不到“$lang”变量。您可以这样做:
include "en-EN.php";
include "~/User.classs.php";
$user = new User($lang);
$user->showIndex();
class User {
protected $lang;
public function __construct($lang) {
$this->lang = $lang;
}
public function test() {
echo $this->lang['Index'];
}
}
课程应该是这样的:
include "en-EN.php";
include "~/User.classs.php";
$user = new User($lang);
$user->showIndex();
class User {
protected $lang;
public function __construct($lang) {
$this->lang = $lang;
}
public function test() {
echo $this->lang['Index'];
}
}
一般来说,最好看看您的
$lang
变量在函数或方法的范围内不可访问。您必须使用global
来导入它
function showIndex() {
global $lang;
echo $lang['Index'];
}
使用globals不是一个好主意-@gotha Register globals和访问全局范围内的变量不是一回事。使用
global
(一般来说)没有什么错。是的,我在考虑gettext,但是,在这个项目中不需要它。我马上试试你的解决办法!