Luminova Framework

PHP Luminova: Define Application Configuration Classes

Last updated: 2026-11-28 20:57:12

Define Luminova configuration objects with an initialization hook, type checks, and array or JSON export.

The Configuration Abstract Class is a base for defining application configuration objects. Declare your settings in a subclass and use the inherited helpers to access, validate, and export their values.

  • Central base class for all configuration objects
  • Supports both declared and dynamic properties
  • Provides safe access helpers and type validation

Note:

Invoking method that doesn't exists will always return null instead of throwing an exception.


Usage

Extending Configuration

Extend AbstractConfig and override onCreate() to load values from the environment.

// app/Config/App.php

namespace App\Config;

use Luminova\Base\AbstractConfig;

class App extends AbstractConfig
{
    public const EMAIL = '[email protected]';
    public const PHONE = '123456789';
    public ?string $var = null;

    protected function onCreate(): void
    {
        $this->var = (string) env('APP_VAR', '');
    }
}

Accessing Class

Now you can import your configuration class anywhere you would like to use itor register in service to make it available anywhere.

use App\Config\App;

$config = new App();

$config->debug = true; // dynamic property

print_r($config->toArray());

Validate Object

Use isInstanceOf() when a service accepts several configuration types.

$config->isInstanceOf(\App\Config\App::class); // true
$config->isInstanceOf(\App\Config\Database::class); // false

Lifecycle

  1. The __construct() Initialize the configuration instance and trigger the creation hook.
  2. The onCreate() hook is executed immediately after object creation.

Notes:

  • Dynamic properties are supported, but should be used carefully
  • Prefer declared properties for predictable structure
  • Use declared public properties for values that callers should read directly.

Class Definition


Methods

__construct

Initialize the configuration object and invoke onCreate().

public function __construct()

onCreate

Hook executed after object construction.

Override in subclasses to perform custom setup or initialization.

protected function onCreate(): void

isInstanceOf

Check if the current instance matches the expected configuration class.

The check follows PHP’s instanceof rules, including inherited classes and implemented interfaces.

public final function isInstanceOf(string $expected): bool

Parameters:

ParameterTypeDescription
$expectedclass-stringFully qualified class or interface name.

Return:

bool - Returns true if instance matches, otherwise false.


toArray

Get object properties as an associative array.

The default export includes initialized public and dynamic properties, along with initialized protected properties visible to the base class. Passing true also checks for initialized protected properties through reflection. Export only the fields you intend to share when the configuration holds secrets.

public final function toArray(bool $includeProtected = false): array

Parameters:

ParameterTypeDescription
$includeProtectedboolExplicitly inspect protected properties (default false).

Return:

array<string,mixed> - Return associative array of property name-value.

Examples:

$config->toArray();        // Visible initialized properties
$config->toArray(true);   // Also inspect protected properties

jsonSerialize

Return class properties as array when used by json_encode().

This calls toArray() using its default argument.

public function jsonSerialize(): array

Return:

array<string,mixed> - Properties keyed by name.


Dynamic Helpers


__set

Set dynamic properties safely.

public function __set(string $property, mixed $value): void

Parameters:

ParameterTypeDescription
$propertystringProperty name.
$valuemixedValue to assign.

Example:

$config->appName = 'MyApp'; // dynamic

__get

Access properties safely.

Returns the property value if it exists, otherwise null.

public function __get(string $key): mixed

Parameters:

ParameterTypeDescription
$keystringProperty name.

Return:

mixed - Value of the property or null if it is undefined.


__call

Call methods dynamically if they exist.

Returns null instead of throwing an error when the method is not defined.

public function __call(string $method, array $arguments): mixed

Parameters:

ParameterTypeDescription
$methodstringInstance method name.
$argumentsarray<int&#124;string, mixed>Arguments to forward.

Return:

mixed - Method result or null if the method is undefined.


__callStatic

Return null when an undefined static method name is invoked.

public static function __callStatic(string $method, array $arguments): mixed

Parameters:

ParameterTypeDescription
$methodstringStatic method name.
$argumentsarray<int&#124;string, mixed>Arguments supplied by the caller.

Return:

mixed - Returns null for an undefined static method name.


getEmailLogTemplate

Override this static hook in the application logger configuration to supply HTML for log email notifications.

public static function getEmailLogTemplate(
    RequestInterface $request,
    AbstractLogger $logger,
    string $message,
    string $level,
    array $context
): ?string

Parameters:

ParameterTypeDescription
$requestRequestInterfaceCurrent request.
$loggerAbstractLoggerLogger preparing the email.
$messagestringLog message.
$levelstringLog level.
$contextarray<array-key,mixed>Additional log data.

Return:

string|null - Returns custom HTML, or null to use the default template.