Search code examples
phpclassconfigurationconfiguration-files

Is it right to set a config PHP class to keep project settings?


I want to create a config.php file to keep the various configuration values that usually being changed from project to project, and I want to define a class to keep the config values in this file like the following:

class Config {
    const DB_SERVER    = 'localhost',
          DB_NAME      = 'abc',
          DB_USERNAME  = 'admin',
          DB_PASSWORD  = '12345',

          WEBSITE_NAME = 'My New Website',
          IMAGE_DIR    = 'img';
}

and so on, I want to define all values as constants inside the class, and I will call them like the following:

$connection = mysql_connect(Config::DB_SERVER, Config::DB_USERNAME, Config::DB_PASSWORD) or die("Database connection failed..");

I want to know: Is this way of setting the project configuration is right? Does this way have any cons? And if it was wrong, then what the best way to do this?


Solution

  • I've found these 2 articles that are talking about the same topic and I found that they are very useful so I wanted to share them here:

    1- Using PHP classes to store configuration data
    2- Using a PHP Class to Store Configuration

    I hope they help as they did for me.