dongyuan1160 2015-07-29 13:04
浏览 39

PHP未定义的配置文件类

I have Database.php and Config.php in the same location. In Database.php, if I do the following it works

<?php
class Database
{
    const DB_ADAPTER = 'mysql';
    const DB_USER = 'something';
    const DB_PASSWORD = 'something';
    const DB_DATABASE = 'something';
    const DB_HOST = "localhost";
    const DB_CHARSET = "utf8";

    /* All my code */

     $dns = self::DB_ADAPTER . ':host=' . self::DB_HOST . ';dbname=' . self::DB_DATABASE;
            self::$dbLink = new \PDO($dns, self::DB_USER, self::DB_PASSWORD);

}

If I move the constants to Config.php and change the above to the following

<?php

include('Config.php');

class Database
{
    /** @var PDO The shared database link. */
    protected static $dbLink;

    /* All my code */

     $dns = Config::DB_ADAPTER . ':host=' . Config::DB_HOST . ';dbname=' . Config::DB_DATABASE;
            self::$dbLink = new \PDO($dns, Config::DB_USER, Config::DB_PASSWORD);

} 

It fails with a Undefined class constant 'DB_ADAPTER' in Database.php (if i remove DB_ADAPTER, it will fail on the next constant).

Why would this be happening?

Thanks

UPDATE

<?php

/**
 * Class containing configuration details
 */
class Config
{
    const DB_ADAPTER = 'mysql';
    const DB_USER = 'something';
    const DB_PASSWORD = 'something';
    const DB_DATABASE = 'something_portal_db';
    const DB_HOST = "localhost";
    const DB_CHARSET = "utf8";
}
  • 写回答

1条回答 默认 最新

  • doutui2883 2015-07-29 13:18
    关注

    Shouldn't you make them static and public to use without object creation?

    <?php
    
    /**
     * Class containing configuration details
     */
    class Config
    {
        public const DB_ADAPTER = 'mysql';
        public const DB_USER = 'something';
        public const DB_PASSWORD = 'something';
        public const DB_DATABASE = 'something_portal_db';
        public const DB_HOST = "localhost";
        public const DB_CHARSET = "utf8";
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计