I've stored some website configuration data in a config.json
file, with things like database connection parameters and routes. Something like this:
{
"production" : { ... },
"test" : { ... },
"development" : { ... }
}
And the content is loaded with:
$config = json_decode(file_get_contents('config'), true);
However, inspecting some frameworks, I see direct usage of PHP scripts for configuration storage:
<?php
return array(
'production' => array( ... ),
'test' => array( ... ),
'development' => array( ... )
);
<?php $config = (require 'config.php');
Which approach is the best?
There are several advantages to using the config.php approach:
Not sure there are really any advantages to using JSON rather than config.php other than if you had multiple applications written in different languages (perl, python, and php for example) that all needed access to the same shared configuration information. There may be other advantages to JSON, but none come to mind at the moment.
It is generally faster to load from a PHP config file, plus it also supports more features, such as closures and byte code caching (if enabled).
Just a note - PHP has a special function for fast loading .ini files that can parse your configuration file
as mentioned in : PHP parse_ini_file() performance?
it is one of the faster method to load configuration files.
Here is the man for it:
http://php.net/manual/en/function.parse-ini-file.php
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With