PHP Luminova: Define Application Configuration Classes
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
nullinstead 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); // falseLifecycle
- The
__construct()Initialize the configuration instance and trigger the creation hook. - 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
- Full namespace:
Luminova\Base\AbstractConfig - This class implements Luminova\Interface\LazyObjectInterface and
JsonSerializable. - This class is abstract.
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(): voidisInstanceOf
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): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$expected | class-string | Fully 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): arrayParameters:
| Parameter | Type | Description |
|---|---|---|
$includeProtected | bool | Explicitly 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 propertiesjsonSerialize
Return class properties as array when used by json_encode().
This calls toArray() using its default argument.
public function jsonSerialize(): arrayReturn:
array<string,mixed> - Properties keyed by name.
Dynamic Helpers
__set
Set dynamic properties safely.
public function __set(string $property, mixed $value): voidParameters:
| Parameter | Type | Description |
|---|---|---|
$property | string | Property name. |
$value | mixed | Value 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): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$key | string | Property 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): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$method | string | Instance method name. |
$arguments | array<int|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): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$method | string | Static method name. |
$arguments | array<int|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
): ?stringParameters:
| Parameter | Type | Description |
|---|---|---|
$request | RequestInterface | Current request. |
$logger | AbstractLogger | Logger preparing the email. |
$message | string | Log message. |
$level | string | Log level. |
$context | array<array-key,mixed> | Additional log data. |
Return:
string|null - Returns custom HTML, or null to use the default template.