views:

760

answers:

5

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.

+8  A: 

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 wildly understand by the PHP programmers
  • you can have some custom logic, for example using custom configurations for development, other for production, etc

Others frameworks like Django and rails uses a config file which is a script.

Tiago
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.
macinjosh
I cannot see how you´ll use .ini files for a nice tree like structure.
Tiago
+7  A: 

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
http://yaml-online-parser.appspot.com/
Paul Tarjan
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
+14  A: 

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
I had no idea, thanks for this great answer!
macinjosh
Besides, it's amazingly simple to write your own INI parser.
Ates Goral
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
great, I'll use that one in my next project :)
Movaxes
+2  A: 

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
JSON doesn't comment well. Config files require comments.
Gary Richardson
+1  A: 

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 appNameConifg;
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);
 }
Unkwntech
You need an addslashes around $value. And this assumes that your configuration will never have complex data attached to a parameter.
jmucchiello
I ommited the sanitizing code I assumed that would be obvious.
Unkwntech