PHP - Application config file stored as - ini,php,sql,cached,php class,JSON,php array? PHP - Application config file stored as - ini,php,sql,cached,php class,JSON,php array? sql sql

PHP - Application config file stored as - ini,php,sql,cached,php class,JSON,php array?


We use a file called Local.php which is excluded from the SCM system. It contains several constants or global variables. For example:

// Local.phpclass Setting{   const URL = 'http://www.foo.com';   const DB_User = 'websmith';}

And it can be referred to anywhere simply by:

Setting::URL

If you need the settings to be writable at runtime, I suggest you use public static variables instead.


The best thing you can do is the simplest thing that could possibly work (php variables) and wrap it up in a class. That way you can change the implementation later without changing any client code. Create an interface that the configuration class implements and make the client code use the interface methods. If you later decide to store configuration in a database or JSON or whatever, you can simply swap out the existing implementation with a new one. Make sure your configuration class is testable and write unit tests.


Try to use php-arrays config files using technique described here: http://www.dasprids.de/blog/2009/05/08/writing-powerful-and-easy-config-files-with-php-arrays

This method allows you to write app configuration in this way:app.config.php

<?phpreturn array(  'appname' => 'My Application Name',  'database' => array(    'type' => 'mysql',    'host' => 'localhost',    'user' => 'root',    'pass' => 'none',    'db' => 'mydb',  ),);

This method is secure, cache-able by opcode cachers (APC, XCACHE).