; our conf.ini file
DEF_APP = home
DEF_BCK = main
DEF_FCT = index
AUTH_ADAPTER = Nov\Auth\Def
driver = pgsql
dsn = "pgsql:dbname=pg1;host=localhost"
username = nov
password = nov
And now we use our ini file:
$conf = parse_ini_file("conf.ini", true);
Another typical configuration file is xml. We also can use built-in functions within PHP when we want to parse xml files. There are also projects that use plain php files, and even yaml files (especially Symfony users)
There are many standard options. Why do I prefer a different one then? I like plain PHP classes because the IDE helps me with autocompletion. The usage is quite simple. Instead of using parse_ini_file function we only need to include our configuration class within our project.
const DEF_APP = 'index';
const DEF_BCK = 'Index\Bck\Main\Page';
const DEF_FCT = 'init';
const AUTH_ADAPTER = "Nov\Auth\Def";
const DB_PG1 = 'PG1';
const DB_PG1_DRIVER = 'pgsql';
const DB_PG1_DSN = 'pgsql:dbname=pg1;host=localhost';
const DB_PG1_USERNAME = 'nov';
const DB_PG1_PASSWORD = 'nov';
Sometimes I want to use a variable stored into the application’s configuration but I don’t remember exactly the name of the variable. If I’ve got the configuration stored into a PHP array from a ini file (or xml, yaml, … ). I need to open the configuration file and find the name of the variable to use in my code. With a PHP class IDEs like Netbeans of VIM will pop up an autocompletion hint with the name of the variableAn example of coding autocompletion with Netbeans
And that’s all. Maybe the worse part of this technique is if our application writes the configuration file (a typical installer.php). In this case we need to build the PHP’s class file and it’s slightly more complicated than writing a simple ini file.
What do you think about it? Do you use another different way?