NotionCommotion Posted October 4, 2020 Share Posted October 4, 2020 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? Keep with a ini file with parse_ini_file. Obviously, not working for me. 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. YAML. Don't think I want to. XML. Not as readable. In a database. Maybe, but might be harder to manage. Hardcode an array in PHP. Probably not. JSON. I like the idea, but feel comments are important in a config file, and am considering the following: Add extra valid JSON properties which contain comments. Don't like the idea. Use JavaScript's JSON.stringify. Too much mixing technologies. 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. Any other ideas? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/311563-how-to-store-configuration-settings/ Share on other sites More sharing options...
benanamen Posted October 4, 2020 Share Posted October 4, 2020 (edited) 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 Laravelhttps://github.com/vlucas/phpdotenv Edited October 4, 2020 by benanamen 1 Quote Link to comment https://forums.phpfreaks.com/topic/311563-how-to-store-configuration-settings/#findComment-1581728 Share on other sites More sharing options...
NotionCommotion Posted October 4, 2020 Author Share Posted October 4, 2020 Thanks benanamen, While it is typically hardcoded , suppose so is a config.ini or config.json file, and as long as a single dedicated file, makes perfect sense to me. Ah, didn't think of dotenv, and think it could be a great approach as well. Quote Link to comment https://forums.phpfreaks.com/topic/311563-how-to-store-configuration-settings/#findComment-1581731 Share on other sites More sharing options...
kicken Posted October 4, 2020 Share Posted October 4, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/311563-how-to-store-configuration-settings/#findComment-1581735 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.