roolith / config
PHP config class
Requires
- php: ^8.0
Requires (Dev)
- phpunit/phpunit: ^9.6
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-09-04 22:34:41 UTC
README
PHP config class
Install
composer require roolith/config
Note: the library supports PHP ^8.0 (runtime and dev toolchain). Dev toolchain uses PHPUnit ^9.6, which also runs on PHP 8.0. The committed lock is resolved against a PHP 8.0 platform, so composer install works on PHP 8.0 and up.
Doc
Project directory requires a folder (e.g config) where configuration varibles will be stored.
Default config filename config.php and environment specific file names are -
development.config.php
production.config.php
Note: default.config.php is reserved and ignored, since config.php is already loaded under the default key. Note: local uses only config.php; a local.config.php file is ignored.
config.php
<?php return [ 'database' => 'generalDatabase', 'username' => 'generalUsername', 'password' => 'generalPassword', 'test' => true, 'nullable' => null, 'log' => [ 'path' => 'generalLogPath', ], ];
Same keys are used in demo/config/config.php and tests/config-test/config.php (demo and test fixtures stay in sync; production.config.php in both adds a.b => c).
production.config.php
<?php return [ 'database' => 'productionDatabase', 'username' => 'productionUsername', 'password' => 'productionPassword', 'a' => [ 'b' => 'c' ] ];
Note: Checkout demo folder more details.
Usage
<?php use Roolith\Configuration\Config; define('ROOLITH_CONFIG_ROOT', __DIR__. '/config'); print_r(Config::get('database')); // generalDatabase
Once environment variable is set
<?php use Roolith\Configuration\Config; require_once __DIR__. '/../vendor/autoload.php'; define('ROOLITH_CONFIG_ROOT', __DIR__. '/config'); define('ROOLITH_ENV', 'production'); // set environment varible // Config::setEnv('development'); // another way to set env var_dump(Config::get('database')); // result will be `productionDatabase` var_dump(Config::env()); // production
More usage
Config::setEnv('production'); Config::get('a.b'); // c Config::get('staging.database', true); // true means it will skip auto set environment
Note: with an active env, dotted keys first try env.key, then fall back to a literal lookup of the full dotted path. So Config::get('staging.database') under production still resolves the literal staging.database when production.staging.database is missing. Pass true as second arg to skip the env-prefixed attempt entirely.
Merge semantics
Per-key shadowing, no deep merge:
Config::get('database')underproductionreturns the production value when present, else theconfig.phpvalue. The env file is checked beforeconfig.php, so a top-levelproductionkey inconfig.phpnever shadows the env file.Config::get('log.path')falls back toconfig.phpwhen the env file has nologkey (seedemo/index.phpandConfigFallbackTest).- Missing keys return silent
null; storednullvalues are preserved (including an envnullshadowing a non-null default). - Fetching a parent array that exists in the env file returns that env array as-is; default children are not deep-merged into it.
- A bare env name returns its file array:
Config::get('staging', true)resolvesstaging.config.php. - When the first dotted segment names an env file, that file wins over
config.php(e.g.staging.database). demo/config/andtests/config-test/use the same fixture keys so the fallback examples behave identically in both.
Environment precedence
Highest first:
Config::setEnv('production')(process envROOLITH_ENVIRONMENT, takes effect immediately).ROOLITH_ENVconstant, read once on first init.localdefault.
Config::reset(); // clear singleton, loaded data, and env state (e.g. for tests) Config::reset(false); // force re-init from the current root while preserving env
A different ROOLITH_CONFIG_ROOT requires a fresh process because PHP constants cannot be redefined in-process.
Note: Config::reset() is a test and reload utility on the concrete class only. Note: it is not part of the ConfigInterface consumer contract.
Upgrade note (breaking change)
The generic environment process env var is no longer read. If you set env via putenv('environment=...'), switch to one of these:
Config::setEnv('production'); // or define('ROOLITH_ENV', 'production'); // or putenv('ROOLITH_ENVIRONMENT=production');