15

I am creating a framework in PHP and need to have a few configuration files. Some of these files will unavoidably have a large number of entries.

What format would be the best for these config files?

Here is my quantification of best:

  1. Easily parsed by PHP. It would be nice if I didn't have to write any parsing code but this is not a deal breaker.
  2. Remains easily read by humans even when there are a large amount of entries
  3. Is a widely used standard, no custom formats
  4. Succinctness is appreciated

I started out using XML, but quickly gave up for obvious reasons. I've thought of JSON and YAML but wanted to see what else is out there.

BinaryMisfit
  • 29,219
  • 2
  • 37
  • 44
joshwbrick
  • 5,882
  • 9
  • 48
  • 72
  • 1
    "I need some sort of tree like structure," is part of another comment. Please update the question with this additional information. Folks may change their answers with your additional facts. – S.Lott Jan 25 '09 at 00:07

5 Answers5

19

How about an INI file format? It's a de facto configuration file standard, and PHP has a built-in parser for that format:

parse_ini_file

Jeff
  • 21,744
  • 6
  • 51
  • 55
  • Besides, it's amazingly simple to write your own INI parser. – Ates Goral Jan 25 '09 at 01:11
  • 5
    To achieve a tree-like hierarchy, the INI file can have sections with dot-delimited names such as [persistence.connection], [persistence.cache], [users.admin], [users.security] etc. – Ates Goral Jan 25 '09 at 01:31
  • great, I'll use that one in my next project :) –  Jan 25 '09 at 02:02
14

YAML is a good option: http://www.yaml.org/

It's very simple and powerful, too

Ruby projects use it a lot for configuration.

nicholaides
  • 19,211
  • 12
  • 66
  • 82
  • It is also VERY slow compared to PHP array, INI files, XML. I read a benchmark test on all these including YAML. While the big 3 were performing around 3,500 request per second, YAML could only do around 850 with the same tests data and system. – JasonDavis Jan 22 '10 at 21:18
12

Why don´t you use a PHP file for the configuration?

The benefits are clear:

  • possible errors will get caught automatically
  • no need to use/create a custom parser
  • it´s widely understood by PHP programmers
  • you can have some custom logic, for example using custom configurations for development, others for production, etc

Other frameworks like Django and rails use a config file which is a script.

Byson
  • 540
  • 4
  • 19
Tiago
  • 9,457
  • 5
  • 39
  • 35
  • The actual data in the config file don't lend well to this solution. The config files will contain definitions for different types of data and each type could possibly have 20+ entries. I need some sort of tree like structure, and the thought of typing this all out into PHP arrays seems daunting. – joshwbrick Jan 24 '09 at 23:53
  • 5
    I cannot see how you´ll use .ini files for a nice tree like structure. – Tiago Jan 25 '09 at 00:38
4

Another option would be to use JSON and use json_encode and json_decode.

You would be able to use richer data structures in your configuration parameters.

Naum
  • 31
  • 2
1

Personly I like to do config data in a class.

class appNameConfig {
    var $dbHost = 'localhost';
    var $dbUser = 'root';
    //...
 }

then to use them all you have to do is

$config = new appNameConfig;
mysql_connect($config->dbHost, $config->dbUser, $config->dbPassword) or die(/*...*/);

to change the config all you have to do is read the file with the class in it I use a function like this:

function updateConfig($parameter, $value) {
    $fh = fopen('config.php', 'w+');
    while(!feof($fh)) {
        $file .= fgets($fh);
     }
    $fileLines = explode("\n", $file);
    for($i=0;$i<count($fileLines);$i++) {
        if(strstr($fileLines[$i], $parameter)) {
            $fileLines[$i] = "$" . $parameter . " = '" . $value . "'";
         }
     }
    $file = implode("\n", $fileLines);
    fwrite($fh, $file);
    fclose($fh);
 }
DBrown
  • 5,111
  • 2
  • 23
  • 24
UnkwnTech
  • 88,102
  • 65
  • 184
  • 229