This is a basic configuration/.ini
file parser built with PHP. It's designed to be error-safe, and provides the opportunity to use a default value if one is not found.
composer require amattu/IniParser
Composer project usage
require(__DIR__ . "/vendor/autoload.php");
$config = new amattu\IniParser(__DIR__ . "/conf.ini");
Without composer
require(__DIR__ . "/src/IniParser.php");
$config = new amattu\IniParser(__DIR__ . "/conf.ini");
The class constructor accepts the file location (path + name), and an optional boolean argument on parsing the individual sections (usually denoted by [NAME]
).
/**
* Configuration reader constructor
*
* @param string $file The path to the configuration file
* @param ?bool $sections Whether to parse individual sections or not
* @throws TypeError
* @author Alec M.
*/
public function __construct(string $filename, ?bool $sections = false)
The get
function will attempt to pull the value from the section specified (... if specified), and if not found, will return the default parameter. All of the arguments except $key
are optional.
$usernameOrDefault = $config->get("USERNAME", "GMAIL_SECTION", "defaultUserName");
/**
* Get configuration file property
*
* @param string $key The property to get
* @param string|null $section The section to get the property from
* @param ?mixed $default value to return if the property is not found
* @return $config[$key] || $default || null
* @throws TypeError
* @author Alec M.
*/
public function get(string $key, ?string $section = null, ?mixed $default = null) : mixed
- PHP 7.x +