views:

722

answers:

6

I've got an app written in PHP, and there are a number of configurable variables. We're implementing a feature where the user can create sets of configurations and easily switch between them. How should I store my configs? as XML? in a .ini file? in multiple .ini files? in a db?

What's going to provide the most flexibility if we add fields down the road? Coding convenience?

If I use a db, I'll have to use a separate one from the app's main one for reasons that aren't worth going into, which has made me shy away from that. (Also, we're using .mdb files for our db.)

I've been going down the xml route, but I'm having problems with adding and editing configs using SimpleXML. Also, the app has to be compatible with php 5.1.6, and I'm a little nervous about some of the functionality.

Never really dealt with creating custom ini files....

A small clarification: the users won't touch the files -- they're non-techie. So setting up the initial files will be done by us, but then we're writing a tool that is going to writie out their configuration(s) to whatever format we choose.

+1  A: 

I think the answer here is going to really depend on your individual situation. There are cases where each of these would be the better option. For your particular case, xml would probably be the best. But where xml really shines over an ini file is if you have multiple related options. For instance if you need to hold on to connection information for a database having nodes for the different parts of the connection underneath a node for that particular connection can be easier to read and maintain. It really depends on the data.

I would also say an xml file would be easier to work with if you were just going to pull out a single value and not load the entire file into memory at the beginning of the application.

Ryan Guill
+2  A: 

Actually, I prefer a runnable PHP file for configuration. You can do lots of things in a runnable file that you cannot accomplish easily using the other solutions. The only big drawback to a runnable file is the admin has to be PHP savvy in order to edit the file safely. PHP can also describe complex data types in your configuration since - it's code.

DB is a problem because you still need an external configuration file to find and connect to the db.

XML is overkill for config.

INI files work and you can use the PHP ini libraries to read them. See parse_ini_file(). ini files don't deal with complex data type very well though.

EDIT in answer to Itay Moav (comments won't format this right):

<?php /* my config file */

$_CFG = Array(); /* problem solved */
$_CFG['key1'] = 'value1';
?>
jmucchiello
and you risk to have dirt in your global scope, and if by accident global-register is on, then you have serious security threat.
Itay Moav
Your configuration is probably going to be in the global scope no matter what. That's why it is configuration. And a simple $_CFG = Array(); at the top of the config file solves the register_globals issue.
jmucchiello
we really don't have to worry about security - this is being compiled into a locally run app.
sprugman
Seems like for this solution, I'll have to write a custom CRUD routine to keep the file updated. I was hoping to be able to use something more automatic.
sprugman
The php file is for stuff that doesn't change. It is admittedly a write-once format. You could of course do both php and db (or ini and db or...) Without knowing the specifics it's hard to know what is or isn't a good idea.
jmucchiello
+1 thanks for parse_ini_file, didn't know about this.
Marco Demajo
+4  A: 

I would store the configurations items in a PHP file. This is the most flexible method I have used.

$config['website_url'] = 'google.com';

I then have the following file structure

- config/development
- config/production

I have a constant defined early in the software named IN_PRODUCTION, when this is TRUE it loads up the production configuration items and vice versa.

This is simple and easily maintainable.

-Mathew

The Pixel Developer
Note that if you store your configuration variables in an ini file, you can get the data into an associative array quite easily. $config = parse_ini_file("config.php");
tj111
I know about parse_ini_file(), but how to I write out a modified config to the file? (I've seen various classes and such in the comments on php.net, but haven't found a native function that does that.)
sprugman
I've read somewhere that the json_decode and file_get_contents functions used together are practically as fast as the parse_ini_file function on its own. Which makes me wonder why json would not be a more frequent option to store config in php as well. It certainly is friendlier than xml and more succinct than ini. Also, json is type aware, i.e. decimal numbers can be parsed as numbers rather than strings. They could probably come up with a parse_json_file function that would be even faster in a future revision of php.
mike
A: 

I would go for ini files, They are human readable, they are being parsed very fast (and if you caching....).
Certainly not XML. Less human readable, parsed not so fast.
I wouldn't do DB due to the speed.
I wouldn't use PHP vars due to it makes you use the GLOBAL scope...

Itay Moav
+3  A: 

Storing the settings for one DB in a different DB does not make much sense, IMO.

I would choose either an ini file, an xml file, or a php file, as proposed by jmucchiello. Each option has it's pros and cons. As for xml versus ini, xml offers more flexibility but is more difficult to maintain (less readable). If your needs are covered by ini, stick with the simpler solution. Use xml only if you need more than ini files can offer you.

As for the php solution, I would rate the readability somewhere between ini and xml, but it would be more difficult to save the current settings. So as much as I like the approach, for your current scenarion I would not recommend it.

Treb
A: 

Write your own configuration class which loads config files. A good example is the way CodeIgniter handles configuration.

http://codeigniter.com/user_guide/libraries/config.html

It loads some config files by default or you can load a specific one with the Config class.

You can download the source code and take a look at how they do it for educational purposes.

Another option would be to generate a php file with a config array from database. So you can administer all the options and on saving generate the file.

Sander Versluys