PHP config file: .ini vs .xml vs .php

Well lately I’ve been thinking about how the new version of my software should use for site global configurations. Right now it uses PHP constants generated from script installer, which is stored in a PHP file. The structure kinda looks like this:

define('DBHOST', 'localhost');             //DB Hostname
define('DBUSER', 'yourdbuser');             //DB Username
define('DBPASS', 'yourdbpassword');             //DB Password
define('DBNAME', 'yourdbname');             //Your database name
define('DOMAIN', '');             //Your domain name (No http, www or . )
define('SCRIPTPATH', '/scriptpath');     //The folder you installed this script in
define('PREFIX', 'prefix_');

I also heard that some applications use .ini file for site configuration, and since PHP has built-in support for parsing .ini file, the operation is fast and convenient. Also storing configuration vars in .xml file seems to be a well-established standard too for some applications. I am a bit hesitant now though, I do not know what to choose? So from the aspects of performance, security, and professionalism, which way is better to define and store configuration vars in a config file? Will you recommend the .php way, or the .ini, .xml way? What is your reason? Thanks, I’d very appreciate inputs from advanced PHP coders.

I don’t have a recommendation, but I’d like to throw in that a PHP class file works great for site settings too!

I’m using this approach for a tiny project I’m working on right now. Basically, you define a static class that you simply include().

class Settings{
    static $db = [
        'host'  => 'localhost',
        'user'  => 'username',
        'pass'  => 'password',
        'domain'=> ''
    static $path = [
        'root'  => '/',
        'js'    => '/assets/js',
        'css'   => '/assets/css'

To use it, you simply do Settings::$db['root']. You can even add methods, like Settings::get() to add features like defaults, sanitization, etc. This method is much more dynamic than your typical settings file and doesn’t require any data processing.

How 'bout all the above? Plus Yaml too. Allow the user to use whichever format they like.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.