jsc-php / configs
Config file parser and writer
Fund package maintenance!
v0.1.1
2026-03-05 13:21 UTC
Requires
- php: ^8.5
- ext-dom: *
- ext-simplexml: *
- ext-yaml: *
This package is auto-updated.
Last update: 2026-03-05 13:32:48 UTC
README
A simple and flexible PHP library for parsing and writing configuration files. Supports JSON, YAML, INI, and XML formats with ease.
Features
- Multi-format Support: Seamlessly handle
.json,.yaml,.ini,.php, and.xmlfiles. - Magic Access: Access and modify configuration data using PHP magic properties.
- Auto-save: Automatically persists changes back to the file when the object is destroyed (can be disabled)1.
- Format Conversion: Easily convert configuration files between supported formats.
- Type Safety: Built for PHP 8.5+ with modern syntax.
Installation
Install via Composer:
composer require jsc-php/configs
Note: Requires the ext-yaml PHP extension for YAML support.
Usage
Basic Example
use JscPhp\Configs\Config; // Load a config file (format is determined by file extension) $config = new Config('test.php'); // Get values using magic methods $db = $config->database; $db_host = $config->database['host']; // Get values using get function $db = $config->get('database'); $db_host = $config->get('database')['host']; $db_host = $config->get('database','host'); $db_host = $config->get('database.host'); // Set values $config->debug = true; $config->set('debug', true); $config->set('debug.level', 'info'); $config->set(['debug','level'], 'info'); // Changes are automatically saved when $config goes out of scope
Options
You can disable autosave in the constructor:
$config = new Config('config.yaml', ['autosave' => false]); $config->theme = 'dark'; // Manual save required if autosave is false $config->save();
Converting Formats
Convert an existing configuration to a different format:
use JscPhp\Configs\Config; use JscPhp\Configs\Types\Type; $config = new Config('settings.ini'); // Save as JSON $config->saveAs('settings.json');
Deleting Keys
$config->delete('key1'); //Deletes array key1 $config->delete('key1.key2'); //Deletes array key2 within key1 $config->delete('key1','key2'); //Deletes array key2 within key1
Supported Formats
- JSON: Uses native
json_encodeandjson_decode. - YAML: Uses
yaml_emitandyaml_parse(requiresext-yaml). - INI: Uses
parse_ini_filewith typed scanning and a custom writer supporting sections and multi-value keys. - PHP: Uses
var_export()to create a return value - XML: Uses
SimpleXMLfor parsing and DOM for formatted output with proper indentation.
License
This project is licensed under the GPL-3.0 License.
Authors
- James Cavaliere - james.cavaliere@gmail.com
Footnotes
-
To save changes, the config file must be writable ↩