Central Configuration
April 11th, 2008 | by admin | |When you begin planning your application, you should immediately think of the common data that may change over time. This may be hard to get your head around at first, but think of things like contact emails that your site may use, or ‘connection to database’ information such as usernames/passwords. You should never hardcode these types of information into the various pages of your application, because if you do decide to change the contact email of your website in a years time, you have to open all pages of your application and either do the replacements manually, or hope a find-and-replace will get all instances throughout all of your files (which, by the way, may not work if you have the contact email entered in a different upper or lower case in different places). And what if you need to change your username/password to your mysql database? If you have 100 class files and functions, all with hardcoded username/password information, this could quickly become a nightmare, for you, and the client who may be paying.
To get around these types of problems, we use a central configuration file to store all this information, then we simple include that configuration file in all our other files, so our commonly used variables are available in our application, and if we ever want to change any configuration data, it’s all available in one easy access file.
An example basic config file is as follows:
– file: config.php
<?php //store the email for our application $CONTACT_EMAIL = 'example@hotmail.com'; //store the database connection information for our application $DATABASE_HOST = 'localhost'; $DATABASE_NAME = 'database1'; $DATABASE_USERNAME = 'root'; $DATABASE_PASSWORD = 'root_password'; ?>
A file in our application can then access this configuration as follows:
<?php
include('config.php');
echo 'This is my website page and this is my contact email from my central configuration data file: '.$CONTACT_EMAIL;
?>