PHP.GT

Working with the Config object

The Config object represents the fully loaded configuration for your application. It is made up of one or more ConfigSection objects, each identified by name.

Constructing a config manually

Although most applications load config from INI files, the object can also be created directly in PHP.

use GT\Config\Config;
use GT\Config\ConfigSection;

$config = new Config(
	new ConfigSection("app", [
		"namespace" => "MyApp",
		"debug" => "true",
	]),
	new ConfigSection("database", [
		"host" => "localhost",
		"port" => "3306",
	])
);

This is particularly useful in tests or when assembling config from another source before writing it out again.

Reading values with dot notation

The most common operation is reading a single value using section and key separated by a dot.

$appNamespace = $config->get("app.namespace");
$dbHost = $config->get("database.host");

If the section or key does not exist, null is returned.

Typed getters

The Config class includes the typed getter methods from PHP.GT/TypeSafeGetter. This means we can ask for the shape of data we expect rather than casting everything ourselves.

$debug = $config->getBool("app.debug");
$port = $config->getInt("database.port");
$ratio = $config->getFloat("cache.ratio");
$launchAt = $config->getDateTime("release.launch_at");

These methods return null when the key is missing.

Accessing sections

If several values belong together, it is often better to access the whole section.

$database = $config->getSection("database");

This returns a ConfigSection object or null if the section does not exist. We will look at sections in detail in the next chapter.

Listing section names

To inspect what is available, call getSectionNames().

foreach($config->getSectionNames() as $name) {
	echo $name, PHP_EOL;
}

Checking production mode

The isProduction() method returns a boolean indicating whether the configuration should be treated as production.

if($config->isProduction()) {
	// production-only behaviour
}

This checks app.production first. If that key was not defined manually, the factory may have set it automatically when config.production.ini was loaded.

Merging configuration objects

Sometimes there is a need to combine two Config objects manually. The preferred way to do this is withMerge().

$combined = $primary->withMerge($fallback);

The naming is important here: the current object keeps its existing values, and the passed config only fills any missing keys or sections. In other words, it behaves like adding fallback values.

For example:

$primary = new Config(
	new ConfigSection("app", [
		"namespace" => "Shop",
	])
);

$fallback = new Config(
	new ConfigSection("app", [
		"namespace" => "DefaultApp",
		"timezone" => "Europe/London",
	])
);

$combined = $primary->withMerge($fallback);

The resulting config will contain:

  • app.namespace = Shop
  • app.timezone = Europe/London

Deprecated mutable merge

There is also a merge() method, but it is deprecated. It mutates the current object and should be avoided in new code.

$config->merge($fallback); // deprecated

Prefer withMerge() so your code stays predictable and easier to reason about.


In the next section we will work more closely with Working with Config Sections.