Luminova Framework

PHP Luminova: Abstract Base Configuration Class

Last updated: 2026-03-19 06:25:29

Base configuration serves as an underlying of your application configuration classes, which may be required by the framework depending on the component used in your application.

The Base Configuration class is the foundation for all config objects in Luminova.It provides a simple, consistent way to define, access, validate, and export configuration 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.


Usages

Extending Configuration

To extend the Configuration class.

// app/Config/AppConfig.php

namespace App\Config;

use Luminova\Base\Configuration;

class AppConfig extends Configuration
{
    public const MY_VAR = 'var';
    public string $name = 'MyApp';
    public ?string $foo = null;

    protected function onCreate(): void
    {
        $this->env = $this->getEnv('APP_ENV', 'production');
        $this->foo = $this->getEnv('FOO_CONFIG');
    }
}

Accessing Class

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

use App\Config\AppConfig;

$config = new AppConfig();

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

print_r($config->toArray());

Validate Object

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

$config->assertInstanceOf(App\Config\AppConfig::class); // passed
$config->assertInstanceOf(App\Config\CliConfig::class); // throw
$config->isInstanceOf(Luminova\Routing\Router::class); // throw invalid class

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 assertInstanceOf() when strict validation is required

Class Declaration


Methods

onCreate

Hook executed after object construction.

Override in subclasses to perform custom setup or initialization.

protected onCreate(): void

isInstanceOf

Check if the current instance matches the expected configuration class.

Validates that $expected is itself a subclass of Configuration,then checks if $this is an instance of it.

public final isInstanceOf(class-string<Configuration> $expected): bool

Parameters:

ParameterTypeDescription
$expectedclass-string<Luminova\Base\Configuration>Fully qualified class name.

Return Value:

bool - Returns true if instance matches, otherwise false.


assertInstanceOf

Ensure the current instance matches the expected configuration class.

Throws if the instance is not of the expected type.

public final assertInstanceOf(string $expected): void

Parameters:

ParameterTypeDescription
$expectedclass-string<Luminova\Base\Configuration>Fully qualified class name to validate against.

Throws:


getEnv

Retrieve an environment variable with optional type casting.

If a type is provided, the value is cast only when it is a string.Otherwise, the raw value is returned.

Supported cast types:

  • bool
  • int
  • float / double
  • string
  • nullable - (empty string becomes null)
protected final getEnv(string $key, mixed $default = null, ?string $return = null): mixed

Parameters:

ParameterTypeDescription
$keystringEnvironment variable name.
$defaultmixedThe default value to return if the key is not found.
$returnstring|nullCast value to expected type (e.g, nullable, float).

Return Value:

mixed - Returns the environment variable cast to the specified type, or default if not found.

Example:

$debug = $this->getEnv('APP_DEBUG', false, 'bool');
$port  = $this->getEnv('APP_PORT', 3306, 'int');

toArray

Get object properties as an associative array.

By default, only public (including dynamic) properties are returned.Set $includeProtected to true to also include protected properties.

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

Parameters:

ParameterTypeDescription
$includeProtectedboolInclude protected properties.

Return Value:

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

Examples:

  • Default: returns public + dynamic properties
  • Optional: include protected properties
$config->toArray();        // public + dynamic
$config->toArray(true);   // + protected

jsonSerialize

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

This internally call toArray(false).

public function jsonSerialize(): array

Dynamic Helpers


__set

Set dynamic properties safely.

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

Example:

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

__get

Access properties safely.

Returns the property value if it exists, otherwise null.

public function __get(string $key): mixed

__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

__callStatic

Call static methods dynamically.

Returns null if the method does not exist.

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