Skip to content

API Reference

Muhammet Şafak edited this page May 29, 2026 · 1 revision

API Reference

The full surface of the package's public types. Behavioural deep-dives are linked into the relevant guides.

Namespace: InitPHP\Config (interfaces under InitPHP\Config\Interfaces, exceptions under InitPHP\Config\Exceptions).

TypeKindSummary
ConfigInterfaceinterfaceThe shared read/write contract.
AbstractConfigabstract classShared implementation of the contract.
Classesabstract classProperties-as-configuration base.
Libraryfinal classMain object + loader API.
Configfinal classStatic facade over a shared Library.
ConfigExceptionclassThrown by loaders on failure.

ConfigInterface

InitPHP\Config\Interfaces\ConfigInterface — the minimal contract every configuration store fulfils. Depend on this in service signatures.

interface ConfigInterface
{
publicfunctionset(string$key, $value): self;
publicfunctionget(string$key, $default = null);
publicfunctionhas(string$key): bool;
publicfunctionremove(string$key): self;
publicfunctionall(): array;
}
MethodReturnsDescription
set(string $key, $value)staticWrite a value; intermediate arrays are created for dotted keys.
get(string $key, $default = null)mixedRead a value, or $default when the key is absent.
has(string $key)boolWhether the key exists (a stored null still counts as present).
remove(string $key)staticRemove a key; a no-op when it is absent.
all()arrayThe entire configuration tree as a plain array.

Keys may use dotted-path notation; see Keys & Dotted Paths.


AbstractConfig

InitPHP\Config\AbstractConfigabstract, implements ConfigInterface. Holds the underlying ParameterBag and implements the shared CRUD + lifecycle. Concrete subclasses populate the store in their constructor. You do not use this type directly; it is the common base for Classes and Library.

abstractclass AbstractConfig implements ConfigInterface
{
protectedParameterBag$parameterBag;
finalprotectedstaticfunctionnewParameterBag(array$data = []): ParameterBag;
publicfunction__destruct();
publicfunctionset(string$key, $value): ConfigInterface;
publicfunctionget(string$key, $default = null);
publicfunctionhas(string$key): bool;
publicfunctionremove(string$key): ConfigInterface;
publicfunctionall(): array;
publicfunctionclose(): void;
}
MemberDescription
newParameterBag(array $data = [])Builds a store in multi (dotted) + case-insensitive mode. Used by every subclass for consistency.
close()Clears the tree by resetting to a fresh empty store; the instance stays usable (options preserved).
__destruct()Calls close().

Classes

InitPHP\Config\Classesabstract, extends AbstractConfig. Turns a subclass's own properties into a configuration tree. See Configuration Classes.

abstractclass Classes extends AbstractConfig
{
publicfunction__construct();
}
  • Imports the default values of the subclass's public and protected properties (private properties are excluded).
  • The internal parameterBag property is never imported.
  • Provides the full ConfigInterface surface inherited from AbstractConfig.
finalclass AppConfig extends Classes
{
publicstring$url = 'http://lvh.me';
publicarray$db = ['host' => 'localhost'];
}
$config = newAppConfig();
$config->get('db.host'); // 'localhost'

Library

InitPHP\Config\Libraryfinal, extends AbstractConfig. The main entry point: the CRUD surface plus loaders and object access. See The Library Object.

finalclass Library extends AbstractConfig
{
publicconstVERSION = '2.0.0';
publicfunction__construct(array$data = []);
publicfunction__get(string$name); // object-style readpublicfunction__debugInfo(): array;
publicfunctionversion(): string;
publicfunctionreplace(array$data): self;
publicfunctionsetArray(?string$name, array$assoc = []): self;
publicfunctionsetFile(?string$name, string$path): self;
publicfunctionsetDir(?string$name, string$path, array$exclude = []): self;
publicfunctionsetClass($classOrObject): self; // string|object
}

Constructor

publicfunction __construct(array$data = []);

Seeds the store with $data in dotted-path, case-insensitive mode.

version

publicfunction version(): string;

Returns the package version string (the VERSION constant, e.g. '2.0.0').

replace

publicfunction replace(array$data): self;

Discards the entire tree and installs $data. Contrast with set(), which writes a single key.

setArray

publicfunction setArray(?string$name, array$assoc = []): self;

Imports an associative array under $name, or merges it into the root when $name is null/''. See Loaders → setArray.

setFile

publicfunction setFile(?string$name, string$path): self;

Loads a PHP file that returns an array. Throws ConfigException when the file is missing, lacks a .php extension, or does not return an array. See Loaders → setFile.

setDir

publicfunction setDir(?string$name, string$path, array$exclude = []): self;

Loads every top-level *.php file in $path. $name is an optional common prefix; $exclude lists base names to skip. Throws ConfigException when $path is not a directory. See Loaders → setDir.

setClass

publicfunction setClass($classOrObject): self; // accepts string|object

Imports the public properties of a class name (defaults) or object (runtime values) under the class's short name. Throws ConfigException when a class name does not exist. See Loaders → setClass.

__get

publicfunction __get(string$name);

Reads a top-level entry as a value (scalar) or a recursively built stdClass (array); returns null for unknown keys. See Object Access.

__debugInfo

publicfunction __debugInfo(): array;

Returns ['version' => …, 'data' => …] so var_dump() shows the version and the current tree rather than internals.


Config

InitPHP\Config\Configfinal, static facade over a shared Library. Cannot be instantiated (private constructor). See The Config Facade.

finalclass Config
{
publicstaticfunction__callStatic(string$name, array$arguments);
publicstaticfunctionreset(): void;
}
MemberDescription
__callStatic()Forwards any call to the shared Library instance.
reset()Discards the shared instance (a no-op if none exists).

The forwarded methods are declared as @method annotations for IDE support:

@method static string version()
@method static mixed get(string $key, $default = null)
@method static Library set(string $key, $value)
@method static bool has(string $key)
@method static Library remove(string $key)
@method static array all()
@method static Library replace(array $data)
@method static Library setArray(?string $name, array $assoc = [])
@method static Library setFile(?string $name, string $path)
@method static Library setDir(?string $name, string $path, array $exclude = [])
@method static Library setClass(string|object $classOrObject)
@method static void close()

ConfigException

InitPHP\Config\Exceptions\ConfigException — extends \RuntimeException. Thrown by the loaders. Each failure mode has a named constructor. See Exceptions.

class ConfigException extends \RuntimeException
{
publicstaticfunctionfileNotFound(string$path): self;
publicstaticfunctionfileMustReturnArray(string$path): self;
publicstaticfunctionnotAPhpFile(string$path): self;
publicstaticfunctionnotADirectory(string$path): self;
publicstaticfunctiondirectoryNotReadable(string$path): self;
publicstaticfunctionclassNotFound(string$class): self;
}

See also

  • Quick Start — the everyday API in five minutes.
  • Loaders — every loader, with examples and error cases.
  • Exceptions — messages and handling patterns.

Clone this wiki locally