Skip to content

Automatically loading config

Greg Bowler edited this page Nov 10, 2026 · 3 revisions

The easiest way to use this library is through ConfigFactory. It looks for known config files, parses each one, and combines them into a single Config object.

use GT\Config\ConfigFactory;

$config = ConfigFactory::createForProject(__DIR__);

Under the hood, file parsing is performed by IniParser, but in most applications there is no need to instantiate the parser directly because the factory already handles file discovery and merge order.

Load order

Files are loaded in this order:

  • config.default.ini
  • config.ini
  • config.dev.ini or config.development.ini, if selected
  • config.deploy.ini
  • the selected staging or production file, if present

Later files take priority over earlier files. Internally, each later file becomes the base configuration, and any missing values are filled from earlier files.

This means that if config.ini contains:

[database]
host=localhost
schema=myapp

and config.dev.ini contains:

[database]
host=dev-db.internal

the final values will be:

  • database.host = dev-db.internal
  • database.schema = myapp

Loading project defaults first

createForProject() accepts a second argument for a default config file outside the project root. This is useful when a framework or shared package wants to supply a baseline config.

$config = ConfigFactory::createForProject(
	__DIR__,
	__DIR__ . "/vendor/example/framework/config.default.ini"
);

Any project file can then override those defaults as usual.

Loading a single file directly

If you want to parse one INI file without the automatic project lookup, call createFromPathName() instead.

$config = ConfigFactory::createFromPathName(__DIR__ . "/config.ini");

If the file does not exist, null is returned.

Environment variable overrides

Whenever a value is read through Config::get(), the environment is checked first. The key name uses underscores instead of dots, so:

  • app.namespace becomes app_namespace
  • database.password becomes database_password

For example, here we can override the password without changing any file:

putenv("database_password=super-secret");
echo $config->get("database.password");

Non-empty environment variable values take precedence over the INI files.

Selecting an environment

Only one environment file may be present in the project directory:

Environment Accepted filenames isProduction() isStaging()
Development config.dev.ini or config.development.ini false false
Staging config.stag.ini or config.staging.ini true true
Production config.prod.ini or config.production.ini true false

The factory checks filenames before parsing or merging configuration. More than one environment file throws EnvironmentConfigurationException, including synonym pairs such as config.dev.ini and config.development.ini. The exception names the conflicting files without exposing configuration values.

config.default.ini, config.ini and config.deploy.ini are ordinary configuration layers and do not count as environment files. Their existing precedence is preserved: development overrides load before deploy overrides; staging and production overrides load afterwards.

Without an environment file, configuration defaults to production. An explicit [app] production=false selects development instead. Staging always retains production behaviour. Empty environment files are valid, so an empty config.dev.ini is enough to select development.

An explicit app.production value must agree with the selected environment. For example, a production or staging file combined with production=false throws an exception. The same rule applies to the app_production environment variable. Environment variables cannot silently change the environment selected by a file.

The production flag accepts true, false, 1, 0, yes, no, on and off, ignoring case and surrounding whitespace. Invalid or blank flags throw an exception. getBool("app.production") follows the same rules as isProduction(); other boolean getters retain their usual behaviour.

Do not commit development configuration containing secrets. Before deploying, remove development configuration and supply at most one production or staging file.


Now that the config is loaded, the next step is Working with the Config object.