ini

INI configuration parser and writer with sections, comments and typed getters.

v1.0.0Data formatsbundledimport "ini";
Overview Installation Documentation Examples

Overview

Zero-dependency INI configuration file parser and writer for HolyPHP with sections, comments, quoted values, typed getters, and clear line-numbered syntax errors.

bundled — works offline zero dependencies config sections

Quick start

import "ini";

$raw = "
app_name = MyApp

[database]
host = 127.0.0.1
port = 5432
ssl = true
password = \"s3cr3t;value\" ; inline comment
";

$config = IniConfig::parse($raw);

println($config->get("", "app_name"));              // MyApp
println($config->get("database", "host"));          // 127.0.0.1
println($config->getInt("database", "port"));       // 5432
println($config->getBool("database", "ssl"));       // true
println($config->get("database", "password"));      // s3cr3t;value

$config->set("database", "pool_size", 10);
println($config->stringify());

Installation

In a project

Inside a project (a folder with hphp.yaml, see Projects & packages):

hphp install ini

This adds the library to hphp.yaml, installs it into .hphp/packages/ini/ and pins the exact version in hphp.lock:

require:
  ini: "^1.0"

Use it

import "ini";

ini is bundled with the compiler, so import "ini"; also works in a single file without installing anything, and hphp install works offline.

Outside a project

hphp install ini          # for your user, shared by all scripts
hphp create myapp                 # or start a project, then: hphp install ini
Packageini
Version1.0.0 (follows the compiler)
Entry fileini.hphp
Sourcepackages/lib/ini ↗

Documentation

API Reference

Functions

ini_parse(string $content): array

Parses an INI string into an associative array.

ini_stringify(array $data): string

Serializes an associative array back into a formatted INI string.

Class IniConfig

new IniConfig(array $data = [])

Creates a configuration object from an associative array.

IniConfig::parse(string $content): IniConfig

Parses an INI string and returns a new IniConfig instance.

get(string $section, string $key, $default = null)

Returns the value for $key in $section (pass "" for global keys), or $default if not found.

getInt(string $section, string $key, int $default = 0): int

Returns the integer value for $key in $section, or $default if not found. Throws an Exception if the value is not a valid integer.

getBool(string $section, string $key, bool $default = false): bool

Returns the boolean value for $key in $section, or $default if not found. Accepts true/false, 1/0, yes/no, on/off (case-insensitive). Throws an Exception if the value is not a valid boolean.

getSection(string $section): array

Returns all key-value pairs in $section (or all global keys when $section === ""). Returns [] if the section does not exist.

has(string $section, string $key): bool

Returns true if $key exists in $section.

hasSection(string $section): bool

Returns true if $section exists.

set(string $section, string $key, $value): void

Sets $key to $value in $section (creating the section if needed). Pass "" for global keys.

stringify(): string

Serializes the configuration to an INI string.

toArray(): array

Returns the underlying associative array.

Error Handling

All syntax and validation errors throw Exception with clear messages prefixed with ini::

Examples

01Quick start

Bundled with the compiler — import "ini"; works offline in any file, or add it to a project with hphp install ini.

import "ini";

$raw = "
app_name = MyApp

[database]
host = 127.0.0.1
port = 5432
ssl = true
password = \"s3cr3t;value\" ; inline comment
";

$config = IniConfig::parse($raw);

println($config->get("", "app_name"));              // MyApp
println($config->get("database", "host"));          // 127.0.0.1
println($config->getInt("database", "port"));       // 5432
println($config->getBool("database", "ssl"));       // true
println($config->get("database", "password"));      // s3cr3t;value

$config->set("database", "pool_size", 10);
println($config->stringify());
Try it
hphp install ini
hphp run main.hphp

Covered by the official test suite in tests/positive/lib_ini.hphp.