PHP config
last modified February 16, 2025
PHP config tutorial shows how to create configuration files in PHP. It uses
the hassankhan/config package.
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.
{
"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.
{
"app": {
"port": 3000
},
"db": {
"host": "localhost",
"port": 27017,
"name": "ydb"
}
}
We have db.json in the config directory.
<?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
PHP YAML example
In the second example, we read configuration data from a YAML file.
app: port: 3000 db: host: localhost port: 27017 name: ydb
This is db.yaml file.
<?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
Merging configuration files
The merge method groups configuration files.
app: port: 3000 db: host: localhost port: 27017 name: ydb
This is the first configuration file.
version: 2.0-dev
This is the second configuration file.
<?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.
<?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.
<?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.
Source
The hassankhan/config repository
In this article we have shown how to read configuration files in PHP
with hassankhan/config package.
Author
List all PHP tutorials.