Application configuration and PHP - the basics

Every medium (or larger) application needs a bit of configuration variables, the only questions are the scale and the desire of the developer to spend more than a minute building it.

So lets make it quick.

I'm going to show 3 basic concepts of wide-system configuration storage with PHP.

Method 1: Plain PHP Array

This maybe the most simple and efficient method of all. Just keep your configurations inside a PHP array and use it globally across your app. Simple to use, fast to develop.

Consider this configuration file:

--config.php-- 
<?php 
$config = array(); 
$config['dbhost'] = 'server1';
$config['dbuser'] = 'bob';
$config['dbpass'] = '1234';
?>

Now, all needed is to include this file inside a page scope and use $config as a global variable inside classes and functions across the application. Like this:

<?php
require_once 'config.php';
if (!$config) die('...');
function connectToDatabase(){
    global $config;
    $conn = mysql_connect( $config['dbhost'] , $config['dbuser'] ,
                                    $config['dbpass'] );
    ...
}
?>

So this method works alright, but it uses global variable which is highly difficult to maintain while our application scales up. Furthermore, I personally dislike the idea of code-configuration hybrid since messing up with configuration should not be involved with code understanding. This type of configuration must be maintained by developers, and developers only and may be used only by PHP speaking apps.

Method 2: XML

So lets move on and separate our configuration from code. The first choice for holding parse-able and dummy-readable structured info is no other than XML. So I guess we all know XML by now. if you don't, click here (but shame on you, really).

Lets try to represent the same configuration with XML document:

---config.xml---
<config>
    <database>
        <host>server1</host>
        <user>bob</user>
        <pass>1234</pass>
    </database>
</config>

And we continue by reading and using the configuration file inside PHP, like this:

<?php
$config = @simplexml_load_file('config.xml');
if (!$config) die('...');
function connectToDatabase(){
    global $config;
    $conn = mysql_connect(
        $config->database->host,
        $config->database->user ,
        $config->database->pass
    );
    ...
}
?>

The implementation is quite similar, expect the data itself that is now represented as a structured XML in a separated document. Using XML as a configuration document may be a good practice, specially when the data is hierarchical. XML allows us to define our configuration variables organized however we want. In practice, we can represent the configuration of a huge application in one file.

However, XML holds some overhead as the amount of text needed to represent tens or more of variables is enormous and a not so human friendly.

Method 3: INI file

Hence, we come to our last method for this post - the INI file. INI is just another way to represent variables as text. It is, however, simple, short and convenient. Lets start with a similar example:

---config.ini---
[database]
db.host = server1
db.user = bob
db.pass = 1234

The [database] title is called a section, and it may or maybe not used while parsing this file. Each line is formed with "KEY = VALUE" pattern. In the above example, db.host is a key and server1 is its value. The dot in the middle has no meaning, it's just a convention.

So lets use this file:

$config = @parse_ini_file('config.ini');
if (!$config) die('...');
function connectToDatabase(){
    global $config;
    $conn = mysql_connect( 
        $config['db.host'] ,
        $config['db.user'] ,
        $config['db.pass']
    );
    ...
}
?>

As you can see, it's pretty similar to the previous examples, we just used INI file instead and we still use the global $config variable all over the place.

Which method should you use? Its really a question of convenient. I didn't test any of those for performance, but I bet that using method 1 is the fastest. If you happen to check it, don't forget to tell me... I personally prefer keeping my configuration variables with INI file, I find it much more simple to view and maintain.

Getting rid of the global $config variable will be on my next post, so hang on.

 

 

Tags: