Jump to content

How to store configuration settings.


NotionCommotion

Recommended Posts

Every now and then, I experience some strange behavior and eventually trace it back to parse_ini_file's scanner_mode being applicable to some parameter but not another, and am looking for alternatives.  Below are several of my thoughts.  How do you store configuration settings?  Do you use different approaches based on the application, and if so what criteria governs which you use?

  1. Keep with a ini file with parse_ini_file.  Obviously, not working for me.
  2. An ini file but with a class dedicated to ensuring the data is appropriate.  Seems like too much duplicating content which will result in errors.
  3. YAML.  Don't think I want to.
  4. XML.  Not as readable.
  5. In a database.  Maybe, but might be harder to manage.
  6. Hardcode an array in PHP.  Probably not.
  7. JSON.  I like the idea, but feel comments are important in a config file, and am considering the following:
    1. Add extra valid JSON properties which contain comments.  Don't like the idea.
    2. Use JavaScript's JSON.stringify.  Too much mixing technologies.
    3. Add comments to the JSON and then strip them using a 3rd party parser such as https://json5.org/ or a little regex.  My main issue is inability to auto-format, but this seems viable.
  8. Any other ideas?

Thanks!

 

Link to comment
Share on other sites

This is what I do...

 

config.php

<?php

return [
                  'charset' =>'utf8mb4'
                , 'name' =>'exambuilder'
                , 'username' =>'root'
                , 'password' =>''
                , 'host' => 'localhost'
                , 'options'  =>[
                          PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
                        , PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
                        , PDO::ATTR_EMULATE_PREPARES => false
                ]
]

somefile.php

<?php

$config = require 'config.php';
echo $config['username'];

You could also use dotenv like Laravel
https://github.com/vlucas/phpdotenv

Edited by benanamen
  • Like 1
Link to comment
Share on other sites

When I started out with PHP I'd always use a simple PHP array in a file and include that file, as demonstrated above.  This is very simple and easy to use.  One thing that can be both a benefit and a detriment of this is that the configuration is PHP code.  One the plus side, you can use the code to do some fancy stuff.  On the down side, allowing code to run in the configuration could cause problems.  Typos for example could cause the whole application to break.

As with most people, I eventually moved toward JSON for my configuration files.   The format works well for simple data and is less susceptible to problems.  There's no possibility to inject code via the configuration. Typos may cause issues, but they can be detected and handled gracefully when parsing the file rather having it break the entire application.   As you mentioned though, comments in the file are not supported by default though which is annoying for various reasons.

After having used Symfony for a while, I like the YAML format and would probably consider it for future projects.  It's not natively supported which is a bit of a bummer, but with any reasonably side project you'd likely have other library requirements anyway.

In all cases, I try and keep the configuration file as small and simple as possible.  If the project involves a database, I will generally store as much configuration data in the database as possible.  Having it in the database allows for easier manipulation of the configuration through the application such as a settings page or similar.  The configuration file often ends up only needing to store the database connection details.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.