tags:

views:

335

answers:

8

Hi all, I need a good config file format. I plan on putting to table schema. Like symfony, ruby on rails, etc. What is the best file format for configuration file ? Yaml or XML or JSON encoded file ? Which is the best for this purpose ?

A: 

What do you plan on putting in there?

You have loads of options:

  • XML with custom tags
  • Key-Value pairs on each new line
  • SQLite?
ck
I wouldn't recommend SQLite as a config as you will need an SQLite client tool to set up you application and you also need to be familiar with SQL queries.
RaYell
+8  A: 

*.ini should be pretty good for that

[MyApp]
var1 = "foo"
var2 = "bar"

[MyPlugin]
var1 = "qwe"

You can easily read it using parse_ini_file()

$config = parse_ini_file('/path/to/config/file', true);
echo $config['MyApp']['var2'];
RaYell
+1  A: 

XML is best for this purpose as most programming langauge privide API to read XML files.

Bhaskar
XML is pain to maintain.
Dmytrii Nagirniak
+1  A: 

Joomla recently switched from ini files to JSON encoded files. Encoding and decoding json using json_encode() and json_decode() performs better than reading and writing ini files.

There is a drawback however: json is not easily human readable. You need to use a php script to encode your config file. You can use this solution well when you want to write your config in an administrator part of your website.

Scharrels
I would say JSON is pretty much human-readable (in contrast to the standard PHP serialization format).
Adam Byrtek
+5  A: 

JSON is a good choice, in my opinion. You could define your database schema as such:

{
    table1: {
        id: {
            type:"int",
            autoincrement:true
        },
        some_field: {
            type:"string",
        }
    },
    table2: { // etc
    }
}

Then just use json_decode to turn this into a PHP array.

e.g.

$tables = json_decode($json_text);
foreach ($tables as $tablename => $t) {
    foreach ($t as $fieldname => $field) {
        echo "Table {$tablename} has a record called {$fieldname}";
    }
}

This would print: Table table1 has a record called id Table table1 has a record called some_field

JSON is much easier to work with than XML in my opinion, and json_encode/decode is very fast so there's little overhead. In my opinion, it's also a lot more readable than XML and copes better with complex data structures than INI files. Some people prefer YAML, but there's really not much difference in syntax.

Rob Knight
Very nice. Thanks. Any idea ?
Zeck
+8  A: 

As others have stated, you have loads of options. Just don't invent (i.e. write parsers) such a thing yourself unless you have a really, really good reason to do so (can't think of any). Just evaluate your options:

  • Do you need to access this data anywhere outside my application (with another language)
  • Do you want users (or myself) to be able to edit the file using a simple text editor
  • Do you need backwards compatibility with older versions of the used language

For example, PHP provides very easy serialize() and unserialize() functions. The format, however, is not easily readable / editable by humans (the same is true for XML). If you ever need to access data with other languages, this is a bad choice. And think about others - I recently hat to parse PHP serialized data from the Horde project in a python project of mine! JSON is a much better choice because its human-readable (if its "pretty-printed", not in the compacted form!), human-editable and every major language has JSON-support nowadays. However, you will lose backwards compatibility, e.g. with python, JSON requires version 2.6+.

Chris089
A: 

my preference is

  1. well documented php file
  2. database for user specific configuration

just like wordpress and countless other php applications use

bumperbox
+1  A: 

The guys have really given you good answers already. Personally I like .ini files outside your document root. Also make sure (if you're on a linux hosting env) that your file is only readable by your web server and not writable.

You can then simply parse the INI file and use your configuration settings like that. (this is if you're using a language such as PHP or so, JavaScript ect won't work here.)

But it's like they said. It all depends on what data you want to have in your config file and what you want todo with it.

Regards Conrad c'',)

Conrad