ZetCode

PHP config

last modified January 10, 2023

PHP config tutorial shows how to create configuration files in PHP. It uses the hassankhan/config package.

$ php -v
php -v
PHP 8.1.2 (cli) (built: Aug  8 2022 07:28:23) (NTS)
...

We use PHP version 8.1.2.

The hassankhan/config is a lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files. If we work with YAML files, we need to install symfony/yaml package.

Setting up PHP config

First, we install the necessary packages.

$ composer req hassankhan/config symfony/yaml

We install two packages with composer.

composer.json
{
    "autoload": {
        "psr-4": {
            "App\\": "src/"
        }
    },
    "require": {
        "hassankhan/config": "^2.0",
        "symfony/yaml": "^4.2"
    }
}

This is composer.json file. We also enable autoloading.

PHP config JSON example

In the first example, we read the configuration data from a JSON file.

config/db.json
{
    "app": {
        "port": 3000
    },
    "db": {
        "host": "localhost",
        "port": 27017,
        "name": "ydb"
    }
}

We have db.json in the config directory.

read_json_cfg.php
<?php

use Noodlehaus\Config;

require('vendor/autoload.php');

// $conf = Config::load('config/db.json');
$conf = new Config('config/db.json');

echo $conf->get('app.port') . "\n";

echo $conf->get('db.host') . "\n";
echo $conf->get('db.port') . "\n";
echo $conf->get('db.name') . "\n";

We load the configuration file either with Config::load or Config. The values are retrieved with the get method. The dot character is used to go through the hierarchy of attributes.

$ php read_json_cfg.php
3000
localhost
27017
ydb

This is the output.

PHP YAML example

In the second example, we read configuration data from a YAML file.

config/db.yaml
app:
  port: 3000
  
db: 
  host: localhost
  port: 27017
  name: ydb

This is db.yaml file.

read_yaml_cfg.php
<?php

use Noodlehaus\Config;
use Noodlehaus\Parser\Yaml;

require('vendor/autoload.php');

$conf = new Config('config/db.yaml', new Yaml);

echo $conf->get('app.port') . "\n";

echo $conf->get('db.host') . "\n";
echo $conf->get('db.port') . "\n";
echo $conf->get('db.name') . "\n";

The example reads configuration file from the db.yaml file.

$conf = new Config('config/db.yaml', new Yaml);

In the second parameter, we provide the configuration parser.

$ php read_yaml_cfg.php
3000
localhost
27017
ydb

This is the output.

Merging configuration files

The merge method groups configuration files.

config/db.yaml
app:
  port: 3000
  
db: 
  host: localhost
  port: 27017
  name: ydb

This is the first configuration file.

config/app.yaml
version: 2.0-dev

This is the second configuration file.

merging.php
<?php

use Noodlehaus\Config;
use Noodlehaus\Parser\Yaml;

require('vendor/autoload.php');

$conf = Config::load('config/db.yaml', new Yaml);
$conf2 = Config::load('config/app.yaml', new Yaml);

$conf->merge($conf2);

echo $conf->get('db.port') . "\n";
echo $conf->get('db.name') . "\n";
echo $conf->get('version') . "\n";

In the example we merge the two configuration files. We can access attributes from both files with one object.

Code configuration with AbstractConfig

We can specify the configuration details in code by using AbstractConfig.

src/Config/AbstractConfig.php
<?php

namespace App\Config;
    
use Noodlehaus\AbstractConfig;

class AppConfig extends AbstractConfig
{
    protected function getDefaults()
    {
        return [
            'host' => 'localhost',
            'port'    => 80,
            'servers' => [
                'host1',
                'host2',
                'host3'
            ]
        ];
    }
}

The configuration is specified in the AbstractConfig's getDefaults file.

code_config.php
<?php

require('vendor/autoload.php');

use Noodlehaus\Config;
use App\Config\AppConfig;

$conf = new AppConfig([]);

echo $conf->get('host') . "\n";
echo $conf->get('port') . "\n";
echo $conf->get('servers')[0] . "\n";

The example reads the configuration from the code.

In this article we have shown how to read configuration files in PHP with hassankhan/config package.

Author

My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

List all PHP tutorials.