Using a Configuration File for an Application

Posted on November 13, 2011 by

Implementing a configuration file in your application will save you time and headaches – now and in the long run.

What is a Configuration File?

Simply put, a configuration file (a “config” file) is a file that stores information about your web application that is … well, configurable. This can include your database credentials, server information, error reporting and root directory information (I also use it to include files which contain utility classes used throughout the system – a lot easier than having all these as include calls on every page of the app).

By having this information in one centralized location (outside a publicly accessible folder if the information is sensitive) you can easily manage external environment and globally needed internal environment information without having to run a global search and replace throughout your code to change things. Also, with one config file you’re only making one include call in all your application files – which makes your code look cleaner.

Why Not Store Specific Info in Specific Files?

You may ask why not store database credentials in a DB file; server credentials in a Server file, etc. While this will work, you’d need to document it for future developers so that they know where to look. Don’t expect that future developers working on your application will know intuitively where you set up your config files just by a file name. Everyone is different and thinks differently.

By having this information in one file, segregated in logical sections, you make it easier for a developer to find everything they need to change settings; and, there’s no guess work on their part as to what is configurable – everything is there for them … just make sure to add comments to your config file to explain things!

In addition to helping developers, you help yourself later on when migrating the application. Imagine migrating an application from development to staging to production. Having multiple files to change would be more work than having just the one file to change.

Also, having a config file means that the only file you need to worry about is that one. The rest of your application should be generic enough that you can bulk upload it from dev to stage, and then from stage to prod without a worry that something will break because a setting is different.

Error Reporting: Effective Use of a Config File

During development, the best errors are the ones that show up on your screen. No digging through a log file or database table; if a mistake is made you see the reason why right away. With a config file you can specify how errors are to be reported. Thus when on dev, errors get reported to the screen; during staging and production errors get sent to a log file and/or database table. By having a flag in your configuration file you can easily switch between one and other other.

Remember those developers who’ll be maintaining and updating your application in the future? They’ll thank you for this. Switching error reporting in one location rather than rummaging through the system to find out if you even set it up this way will help them out.

Things to Remember

Config files are an important part of keeping global variables separate from the rest of the application, allowing your application to be more easily configured for different environments. Just remember the following advice: