dongtigai3875 2013-05-14 12:45
浏览 52

无法使用类中定义的变量

Hi i am trying to import the configuration variables from a config file that i had created

Config.php has

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

for importing this variable in another file i tried

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

But when i try to use $cnfig->DBNAME2 its not working

Please help to sole this problem.

Thank you.

EDIT

It was working in localsystem , when i tied to upload through web hosting it was not working.

  • 写回答

1条回答 默认 最新

  • doulu1968 2013-05-14 13:09
    关注

    Try below one

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

    Edit:

    I am getting below output so it should be working for you too.

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

    Edit:2

    You can also do like this

    (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 has higher priority than include()

    Error Ouput:

    When only use include('./Config.php') or die("error"); it's giving below error.


    Warning: include(1) [function.include]: failed to open stream: No such file or directory in D:\path_to_file\myConfig.php on line 2

    Warning: include() [function.include]: Failed opening '1' for inclusion (include_path='.;D:\ZendServer\share\ZendFramework\library') in D:\path_to_file\myConfig.php on line 2

    Fatal error: Class 'Config' not found in D:\path_to_file\myConfig.php on line 6

    评论

报告相同问题?

悬赏问题

  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题