无法使用类中定义的变量

时间:2013-05-14 12:45:15

标签: php import global-variables

您好我正在尝试从我创建的配置文件中导入配置变量

Config.php

class Config
{
    public $SERVERNAME = '*******';
    public $SUSERNAME = '*********';
    public $SPASSWORD = '*********';
    public $DBNAME2 = '********';
}

用于在我试过的另一个文件中导入此变量

include('./Config.php') or die("error");
$cnfig = new Config();

但是当我尝试使用$cnfig->DBNAME2时它不起作用

请帮助解决这个问题。

谢谢。

修改

它在本地系统中运行,当我通过网络托管上传时,它无效。

1 个答案:

答案 0 :(得分:1)

尝试以下

include  ('./Config.php');
$cnfig = new Config();
echo "<pre>";print_r($cnfig);

修改

我的输出低于输出,所以它也适合你。

Config Object
(
    [SERVERNAME] => *******
    [SUSERNAME] => *********
    [SPASSWORD] => *********
    [DBNAME2] => ********
)

修改:2

您也可以这样做

(include('./Config.php')) or die("error");
$cnfig = new Config();
echo "<pre>";print_r($cnfig);

http://www.php.net/manual/en/function.include.php#39043

or的优先级高于include()

错误输出:

当仅使用include('./Config.php') or die("error");时,它会给出以下错误。


警告:include(1)[function.include]:无法打开流:第2行的D:\ path_to_file \ myConfig.php中没有此类文件或目录

警告:include()[function.include]:无法在D:\ path_to_file \ myConfig.php中打开包含(include_path ='。; D:\ ZendServer \ share \ ZendFramework \ library')的'1'失败2

致命错误:第6行的D:\ path_to_file \ myConfig.php中找不到“Config”类