A simple, framework-agnostic settings container for PHP applications.
Licensed under the MIT license and is free for private or commercial projects.
This library provides a lightweight wrapper around a plain PHP array, giving you a clean interface for storing and retrieving application configuration values. It offers a straightforward, dependency-free way to manage configuration without coupling your code to a specific framework, making it easy to drop into any project or architecture.
composer require andrewdyer/php-settings
Requires PHP 8.3 or newer.
Create a Settings instance by passing in your configuration array.
declare(strict_types=1);
use Anddye\Settings\Settings;
$settings = new Settings([
'app_name' => 'My Application',
'database' => [
'host' => 'localhost',
'port' => 5432,
'credentials' => [
'username' => 'admin',
'password' => 'secret',
],
],
]);
Get the entire settings array using the all() method.
$all = $settings->all();
Access a top-level setting using its key with the get() method.
$appName = $settings->get('app_name'); // 'My Application'
The get() method can also access nested settings using dot notation.
$host = $settings->get('database.host'); // 'localhost'
Dot notation can traverse multiple levels of configuration.
$username = $settings->get('database.credentials.username'); // 'admin'
Requesting a parent key with get() returns the entire nested configuration array.
$database = $settings->get('database');
Check if a top-level key exists using the has() method.
$settings->has('app_name'); // true
The has() method also supports nested keys using dot notation.
$settings->has('database.credentials.password'); // true
If a top-level key contains dots, the exact key takes precedence over nested resolution.
$settings = new Settings([
'database.host' => 'literal',
]);
$settings->get('database.host'); // 'literal'
If the exact key does not exist, the get() method falls back to resolving nested values using dot notation.
$settings = new Settings([
'database' => [
'host' => 'nested',
],
]);
$settings->get('database.host'); // 'nested'