Repository navigation
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.
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.
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.
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.
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.
To inspect what is available, call getSectionNames().
foreach($config->getSectionNames() as $name) {
echo $name, PHP_EOL;
}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.
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 = Shopapp.timezone = Europe/London
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); // deprecatedPrefer 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.
PHP.GT/Config is a separately maintained component of PHP.GT/WebEngine.