PHP Luminova: Abstract Base Configuration Class
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
nullinstead 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 classLifecycle
- 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
assertInstanceOf()when strict validation is required
Class Declaration
- Class namespace:
Luminova\Base\Configuration - This class implements: \Luminova\Interface\LazyObjectInterface, \JsonSerializable
- This class is an Abstract class
Methods
onCreate
Hook executed after object construction.
Override in subclasses to perform custom setup or initialization.
protected onCreate(): voidisInstanceOf
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): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$expected | class-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): voidParameters:
| Parameter | Type | Description |
|---|---|---|
$expected | class-string<Luminova\Base\Configuration> | Fully qualified class name to validate against. |
Throws:
- \Luminova\Exceptions\InvalidArgumentException - If the instance is not of the expected type or
$expectedis not a valid Configuration subclass.
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:
boolintfloat/doublestringnullable- (empty string becomes null)
protected final getEnv(string $key, mixed $default = null, ?string $return = null): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$key | string | Environment variable name. |
$default | mixed | The default value to return if the key is not found. |
$return | string|null | Cast 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): arrayParameters:
| Parameter | Type | Description |
|---|---|---|
$includeProtected | bool | Include 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); // + protectedjsonSerialize
Return class properties as array when used by json_encode().
This internally call toArray(false).
public function jsonSerialize(): arrayDynamic Helpers
__set
Set dynamic properties safely.
public function __set(string $property, mixed $value): mixedExample:
$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