andrewdyer / php-settings
A simple, framework-agnostic settings container for PHP applications
Requires
- php: ^8.3
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.75
- phpunit/phpunit: ^10.5
This package is auto-updated.
Last update: 2026-03-12 14:50:17 UTC
README
A simple, framework-agnostic settings container for PHP applications.
⚖️ License
Licensed under the MIT license and is free for private or commercial projects.
✨ Introduction
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.
📥 Installation
composer require andrewdyer/php-settings
Requires PHP 8.3 or newer.
🚀 Getting Started
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', ], ], ]);
📚 Usage
Retrieve all settings
Get the entire settings array using the all() method.
$all = $settings->all();
Retrieve a setting
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 setting exists
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
Literal keys containing dots
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'