This documentation provides a detailed overview of the Config class, which is designed to manage configuration settings in your application. The Config class allows you to access, set, update, delete, and check the existence of configuration values using both dot notation and array notation.
- Overview
- Class Features
- Creating a Config Instance
- Accessing Values
- Setting Values
- Updating Values
- Deleting Values
- Checking for Existence
- Working with Multiple Values
- Merging Configuration Data
- Converting to Array or JSON
- Iterating Over Configuration Data
- Countable and Array Access
- Examples
- Conclusion
The Config class is designed to simplify the management of configuration settings in your application. It provides a flexible way to work with configuration data, supporting:
- Accessing values using dot notation (e.g.,
'database.host'
) or array notation (e.g.,['database', 'host']
). - Modifying configuration data at runtime.
- Checking the existence of keys.
- Working with nested configuration structures.
- Extends:
ArrayObject
- Implements:
ArrayAccess
IteratorAggregate
Countable
JsonSerializable
By extending ArrayObject
and implementing these interfaces, the Config class provides seamless integration with PHP's array and object handling mechanisms.
You can create a new Config instance by passing an array of configuration data:
use \ItalyStrap\Config\Config;
$config = new Config([
'database' => [
'host' => 'localhost',
'port' => 3306,
'credentials' => [
'username' => 'root',
'password' => 'secret',
],
],
'app' => [
'debug' => true,
],
]);
You can access nested configuration values using dot notation:
$host = $config->get('database.host');
// Output: 'localhost'
If the key does not exist, you can provide a default value:
$charset = $config->get('database.charset', 'utf8mb4');
// Output: 'utf8mb4'
Alternatively, you can access values using array notation:
$host = $config->get(['database', 'host']);
// Output: 'localhost'
You can set configuration values using the set
method with dot notation or array notation:
$config->set('database.host', '127.0.0.1');
// or
$config->set(['database', 'host'], '127.0.0.1');
$host = $config->get('database.host');
// Output: '127.0.0.1'
The update
method works similarly to set
. It sets a value at the specified key:
$config->update('app.debug', false);
$debug = $config->get('app.debug');
// Output: false
You can delete configuration values using the delete
method:
$config->delete('database.credentials.password');
// or
$config->delete(['database', 'credentials', 'password']);
$password = $config->get('database.credentials.password');
// Output: null
To check if a configuration key exists, use the has
method:
$exists = $config->has('database.credentials.username');
// Output: true
$exists = $config->has('database.credentials.password');
// Output: false (since we deleted it earlier)
The Config class provides methods to work with multiple keys at once.
Use getMultiple
to retrieve multiple values:
/** @var mixed[] $values */
$values = $config->getMultiple(['database.host', 'app.debug'], 'default');
// Output:
// [
// 'database.host' => '127.0.0.1',
// 'app.debug' => false,
// ]
Use setMultiple
to set multiple values:
/** @var bool $isSet */
$isSet = $config->setMultiple([
'database.port' => 3307,
'app.env' => 'production',
]);
$port = $config->get('database.port');
$env = $config->get('app.env');
// Output:
// $port = 3307
// $env = 'production'
Use deleteMultiple
to delete multiple values:
/** @var bool $isDeleted */
$isDeleted = $config->deleteMultiple(['database.port', 'app.env']);
$portExists = $config->has('database.port');
$envExists = $config->has('app.env');
// Output:
// $portExists = false
// $envExists = false
You can merge additional configuration data into the existing Config instance:
$config->merge([
'cache' => [
'enabled' => true,
'ttl' => 3600,
],
]);
$cacheEnabled = $config->get('cache.enabled');
// Output: true
You can get all configuration data as an array:
$allConfig = $config->toArray();
// Output:
// [
// 'database' => [...],
// 'app' => [...],
// 'cache' => [...],
// ]
Since the Config class implements JsonSerializable
, you can convert it to JSON:
$jsonConfig = \json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
// Output: JSON representation of the configuration data
Because Config implements IteratorAggregate
, you can iterate over it using a foreach loop:
foreach ($config as $key => $value) {
// Process $key and $value
}
Note: Iterating directly over the Config instance will iterate over the top-level keys.
You can use the traverse
method to traverse nested configuration data, see the Traversing Data documentation for more information.
Since Config implements Countable
, you can count the number of top-level configuration entries:
$count = count($config);
// Output: Number of top-level keys in the configuration
You can access top-level configuration entries using array access:
$databaseConfig = $config['database'];
$host = $databaseConfig['host'];
// Output: '127.0.0.1'
You can access sublevel configuration entries as well, this is a little bit tricky, but you can also access nested using dot notation (here array notation does not work):
$host = $config['database']['host'];
// or
$host = $config['database.host'];
// Output: '127.0.0.1'
You can see if a key exists:
$exists = isset($config['database.host']);
// or
$exists = isset($config['database']['host']);
// Output: true
You can also set top-level configuration entries:
$config['new_setting'] = 'value';
$value = $config['new_setting'];
// Output: 'value'
Or with dot notation:
$config['new_setting.sub_setting'] = 'value';
$value = $config['new_setting.sub_setting'];
// Output: 'value'
But this will not work:
$config['new_setting']['sub_setting'] = 'value';
// Indirect modification of overloaded element of ItalyStrap\Config\Config has no effect
The reason is that the ArrayObject
class does not allow you to set nested values using array notation, this was meant to be used with flat arrays and not nested arrays.
$config = new Config([
'services' => [
'mail' => [
'driver' => 'smtp',
'host' => 'smtp.example.com',
'port' => 587,
'encryption' => 'tls',
'username' => '[email protected]',
'password' => 'password',
],
],
]);
// Accessing values
$mailDriver = $config->get('services.mail.driver');
// Setting values
$config->set('services.mail.password', 'newpassword');
// Deleting values
$config->delete('services.mail.encryption');
// Checking existence
$hasEncryption = $config->has('services.mail.encryption');
// Output:
// $mailDriver = 'smtp'
// $hasEncryption = false
$timezone = $config->get('app.timezone', 'UTC');
// Output: 'UTC' (if 'app.timezone' is not set)
if ($config->get('app.debug')) {
\error_reporting(E_ALL);
} else {
\error_reporting(0);
}
The Config class provides a powerful and flexible way to manage configuration settings in your application. By supporting both dot notation and array notation, and by implementing standard PHP interfaces, it integrates smoothly with your codebase and offers a rich set of features for accessing and modifying configuration data.