Reputation: 5992
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:
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.
Upvotes: 15
Views: 9494
Reputation: 90951
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);
}
Upvotes: 1
Reputation: 9558
Why don´t you use a PHP file for the configuration?
The benefits are clear:
Other frameworks like Django and rails use a config file which is a script.
Upvotes: 12
Reputation: 31
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.
Upvotes: 4
Reputation: 21902
How about an INI file format? It's a de facto configuration file standard, and PHP has a built-in parser for that format:
Upvotes: 19
Reputation: 19509
YAML is a good option: http://www.yaml.org/
It's very simple and powerful, too
Ruby projects use it a lot for configuration.
Upvotes: 14