2

I'm developing a PHP application where I have to store configuration variables.

Using MySQL would be overkill, because it's a CLI app I'm talking about and there are only a couple of configuration variables.

I don't know about INI files... I'm thinking of using SQLite.

What do you think? It is too overkill using SQLite? Do you suggest using other approaches?

3 Answers 3

1

That really depends on how large your configuration files are going to be and how many of them you are going to store.

PHP handles ini files quite well and it seems to be a popular choice amongst PHP frameworks.

Other popular approaches:

  • XML
  • YAML
  • files containing JSON
Sign up to request clarification or add additional context in comments.

1 Comment

Avoid yaml; anything that parses it is either an external module or is implemented in PHP itself (and has horrible performance). ini, json and xml are natively supported and don't carry a perf hit
1

For the configuration values I would suggest you to use INI files and handle it with parse_ini_file function which is a lot more easier way than resorting to SQLlite.

5 Comments

I want to write to the config files too. I saw a lot of method to just read the entire config file, manipulate it and overwrite the entire config file. I think that's not very efficient
@feketegy - How often will you be writing to these config files? I doubt efficiency is going to matter.
Quite often. I have to track update times in these files. This CLI php will be tied to a cron job and ran in every 5 minutes
See : php.net/manual/en/function.parse-ini-file.php#94414 exemple of writing in ini file
that's not a config then, that's a dynamic value and is indeed something for a database.
1

Consider serializing/unserializing an array for small configuration settings:

<?php
# create settings array
$settings = array(
    'setting1' => 'on',
    'setting2' => 'off',
    'setting3' => true,
);
# save it
$filename = 'settings.txt';
file_put_contents($filename,serialize($settings));


# retrieve it
$getSettings = unserialize(file_get_contents($filename));

    # modify it
    $getSettings['addedLater'] = 'new value';

    # save it again.
    file_put_contents($filename,serialize($getSettings));

    # rise and repeat
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.