Reputation: 254
I am developing an PHP/MySQL app. Now I'm writing down a set of data that will serve as application config information. Basically, they won't be changed, period.
I'm in a search of the best practices of storing data like such, but no clear answer so far.
I know it's popular to use XML for that purpose, but you need to parse that in a separate function call and files are slower to read from.
I thought about writing a PHP class to be included as config that everytime the app's run, it's loaded, kind of like environment variables. But is it still a good solution if I have just few, or quite a large number of them?
I know I run MySQL queries on most of my API calls. However, is MySQL a great choice if the data only gets read?
P.S - to be more specific, the config data structure's like:
"Products": [
"1": "Apple",
"2": "Orange",
"4": "Pear"
]
I store keys in the database ("1", "2" or "4") then present in my UI with English words. Am I doing it right with the data structure?
Currently it looks like it's very simple and small data. But I'm expecting it to grow to hundreds of data like this.
Upvotes: 0
Views: 188
Reputation: 324
Personally, I love (and I find them really powerful) PHP file configuration, but for a big scale application I would prefer to keep the code and the configuration in separated areas. I love json and I find it really easy to understand, and PHP can decode it with the built-in function json_decode(). So, IMHO JSON is the best way to create a big configuration file..
Upvotes: 1
Reputation: 18550
If you store it in the database where do your database connection settings go ?
Config files are by far the easiest to work with, they can be put in your version control system to see changes over time etc.
I usually try and use the function parse_ini_file
to lead it in to keep it in a well known format, and make it clear that its not code its a config.
XML is slightly harder as you also have to get the syntax correct, though does offer some more flexibility. So my opinion is only use XML when you have options you can not represent in an INI file
The ini sections work great for seperating live, staging and dev configs also keeping them seperate, but together in a simple and easy to edit way.
Upvotes: 1
Reputation: 313
Try to hold all your configuration data inside core PHP arrays and include the file(s) in beginning of your application.
# Your main application
$conf = include 'conf.php';
With conf.php
returning a PHP array:
return array(
'key1' => 'value1',
);
You can also split the configuration into several files to make things easier while you are developing and testing your application. For example you could create a conf/shared.php
with all the common configuration options. A second File conf/dev.php
will hold all the config options you will need while your application is running on your development machine (like database connection options, log settings, etc.). A third file conf/prod.php
would hold everything which is needed while the application is running on the production environment.
# Your main application
$conf = include 'conf/shared.php';
# Determine the application environment
# Note: You will need to set the environment variable inside the Apache configuration for yourself
if (!isset($_ENV['appEnv'])) {
# Make sure the environment is always set
$_ENV['appEnv'] = 'dev';
}
# Include the needed environment config file and merge it with shared
$conf = array_merge_recursive($conf, include 'conf/' . $_ENV['appEnv'] . '.php');
If I would be you I wouldn't think about caching the configuration at this stage of your application development. Try to write only the things you will need at the moment but write them clear and without errors... It's always possible to implement some kind of config caching later when you really need it. IMO if you stick with core PHP arrays there is no need to do that unless you need to include a lot of files or really big arrays (1000+ entries).
Upvotes: 1
Reputation: 21817
IMHO Application configuration should be read as fast as possible, because it needs to be read on every request.
The best option to achieve that is to use a plain PHP array. There is no faster solution than a plain PHP array in combination with an opcode cache (like APC or Opcache).
But this doesn't mean you have to write that configuration as a plain PHP array. You should use whatever your most comfortable with. XML, YAML, JSON, etc doesn't really matter.
The idea is that the configuration is read from a file using your favorite format, converted to a plain PHP array, then stored as PHP in another file, the first time only. The next time the configuration is needed, the plain PHP array should be read from file. In other words: only if the plain PHP version isn't available, you read the other format and create it.
I recommend to take a look at the Symfony 2 Config component, which facilitates this process.
Upvotes: 1
Reputation: 101
An array stored directly in PHP would be the easiest way to go for this. Hundreds, thousands of rows, no problem. Beyond a thousand, I would recommend putting in a database
You mentioned you see the data set growing. Important questions to consider are: how will you manage this growth, will you be modifying the data manually or generating it. Do you see using a different language later. JSON is very popular and there are efficient parsers available.
Hope that helps.
Upvotes: 0